Search code examples
csvsassas-macro

Mass Convert Multiple SAS datasets into CSV files


I have around 720 SAS datasets (which are equipped with 720 SAS index files ".sas7bdnx") within a single folder that I would like to convert to CSV files. I know how to use proc export to convert one at a time, but any efficient way to do the conversion for all in a one SAS program? All the datasets are stored on a local unix server.


Solution

  • This is fairly simple, contingent upon you having some reasonable way to identify them in code.

    Here is the simple answer:

    %macro makeCSV(dataset=);
    proc export data=&dataset. file="&dataset..csv" dbms=csv replace;
    run;
    *modify export if needed;
    %mend makeCSV;
    
    proc sql;
      select cats('%makeCSV(dataset=',memname,')') into :makeCSVlist separated by ' '
        from dictionary.tables
        where libname='YOURLIB' and memname like 'FORCSV';
    quit;
    *or whatever logic identifies these 720 or whatnot datasets;
    &makeCSVlist; *actually runs the macro calls;
    

    Now, that's probably fairly slow, but it should work. Faster would be not to have 720 datasets, but one dataset, and write out the code in a data step using FILEVAR= option. That becomes a little difficult if you have a huge number of variables (as you do have to write a put statement out) but even then you can generate code sort of like what I just did using dictionary.columns. That would still generate 720 CSVs, but it's much faster because it avoids the 720 proc export calls and their overhead.