Convert data to CSV file

This post was kindly contributed by Ken's SAS tricks - go there to comment and to read the full post.

In addition to the export file wizard and proc export (section 1.2.3 of SAS and R), SAS provides a the nifty little %ds2csv macro that can do this quickly and easily. 

The advantages to this approach are that there are a few extra options and that the documentation is actually more explicit than that provided for proc export.

Example:

data test;
do i = 1 to 20;
x = normal(0);
y = ranpoi(0,10);
z = floor(1000 * uniform(0));
output;
end;
run;

%ds2csv(data=test, runmode = b, csvfile=c:\temp\test.csv);

The runmode option is b if you’re in the program editor. Other options let you customize what vars to include, restrict observations, whether column headers are included, and MIME/HTTP headers. 

Documentations for this little gem is tucked away under: Contents; SAS Products; Base SAS; SAS 9.2 Language Reference: Dictionary; Appendices; SAS Utility Macro.  (Of course!  Where else?)

The documentation claims this only runs within a data step, but the code above shows otherwise.

 

 

This post was kindly contributed by Ken's SAS tricks - go there to comment and to read the full post.