One easy way for creating an ASCII data file from a SAS data set is to
use the put statement in a data step. First of all, we use filename statement to tell
SAS where the ASCII file is going to be
located and what it is called. Then in the data step, we use file
statement to refer to this file and use put statement to write to it.
Here are some examples using data set
hsb2.sas7bdat.
Example 1: Creating a space-delimited file
libname in 'd:datasas'; data hsb2; set in.hsb2; run; filename myfile "d:temphsb2.txt"; *space delimited file; data _null_; set hsb2; file myfile; put id female ses prog; run;70 0 1 1 121 1 2 3 86 0 3 1 141 0 3 3 172 0 2 2 113 0 2 2 50 0 2 1 11 0 2 2 84 0 2 1 48 0 2 2 75 0 2 3 60 0 2 2 95 0 3 2 104 0 3 2 38 0 1 2 115 0 1 1 76 0 3 2 195 0 2 1 114 0 3 2
Example 2: Creating a comma separated file
This can be extended
to any delimiters.
filename myfile "d:temphsb2_comma.txt"; data _null_; set hsb2; file myfile; put id "," female "," ses "," prog; run;70 ,0 ,1 ,1 121 ,1 ,2 ,3 86 ,0 ,3 ,1 141 ,0 ,3 ,3 172 ,0 ,2 ,2 113 ,0 ,2 ,2 50 ,0 ,2 ,1 11 ,0 ,2 ,2 84 ,0 ,2 ,1 48 ,0 ,2 ,2 75 ,0 ,2 ,3 60 ,0 ,2 ,2 95 ,0 ,3 ,2 104 ,0 ,3 ,2 38 ,0 ,1 ,2 115 ,0 ,1 ,1 76 ,0 ,3 ,2 195 ,0 ,2 ,1 114 ,0 ,3 ,2
Example 3: Creating a file with multiple lines per record
filename myfile "d:temphsb2_mlines.txt"; data _null_; set hsb2; file myfile; put id ; put female ses prog; run;70 0 1 1 121 1 2 3 86 0 3 1 141 0 3 3 172 0 2 2 113 0 2 2 50 0 2 1 11 0 2 2 84 0 2 1 48 0 2 2
Example 4: Creating a file with fixed format
filename myfile "d:temphsb2_fixed.txt"; data _null_; set hsb2; file myfile; put id 1-3 female 10 ses 15 prog 20 ; run;70 0 1 1 121 1 2 3 86 0 3 1 141 0 3 3 172 0 2 2 113 0 2 2 50 0 2 1 11 0 2 2 84 0 2 1 48 0 2 2 75 0 2 3 60 0 2 2 95 0 3 2 104 0 3 2