Cornell University Cornell University CISER

CISER Computing

Writing a SAS Dataset To a Text File

Q.  What's an easy was to write a small SAS data set to a space-delimited or a comma delimited text file?

A.  To write a SAS data set to a text file use a DATA step with a FILE statement and a PUT statement. In your FILE statement use the option DLM= to specify a delimiter for the output text file (Note: Output that contains embedded delimiters requires the DSD option). The SAS uses a single space as the default delimiter. (Caution: If your SAS data set includes non-numeric variables with missing values, do not use a space as the output ascii data delimiter.)  Examples are given below.

To Create a Space Delimited Text File:

libname ssd 'u:\proj\' ;
data _null_ ; 
         /* No SAS data set is created */
   
set ssd.income ;
    FILE  'u:\rawfile.txt' ;
     /* Output Text File */
   
PUT var2 var3 var1 var4 var5 ;
run ;

To Create a Comma Delimited Text File:

libname ssd 'u:\proj\' ;
data _null_ ; 
         /* No SAS data set is created */
   
set ssd.income ;
    FILE  'u:\rawfile.txt' DLM=',' ;
    /* Output Text File */
   
PUT  var5 var2 var3 var1 var4 ;
run ;


You can also use SAS Export Wizard to create a delimited ascii file from a SAS data set. To use the wizard, create a SAS data library and then click on FILEExport Data.