Tuesday, June 12, 2012

Create a .CSV file of SAS dataset without column names or header row?



SAS places the variables names in Row 1 when you try to create an excel or .CSV file of the  SAS dataset. I have found a tip to tell SAS not to keep variable names in the row 1 of .CSV file.
SAScommunity.org page has put together nice information regarding how to do this.

  • 1 Run PROC EXPORT with PUTNAMES=NO
  • 2 Run PROC EXPORT and recall and edit the code
  • 3 Run PROC EXPORT and use a DATA step to rewrite the file without the first row
  • 4 DATA _NULL_ with a PUT statement
  • 5 DATA _NULL_ with a PUT statement, all fields quoted
  • 6 ODS CSV and PROC REPORT with suppressed column headers
  • 7 The %ds2csv SAS Institute utility macro
  • 8 The CSV tagset and the table_headers="NO" option
Run PROC EXPORT with PUTNAMES=NO
Sample program 
proc export data=data_to_export  outfile='C:\data_exported.csv'
        dbms=csv
        replace;
        putnames=no;
run;

Run PROC EXPORT and use a DATA step to rewrite the file without the first row

Sample program

filename exTemp temp;

proc export data=sashelp.class outfile=exTemp dbms=csv;
run;
 
data _null_;
   infile extemp firstobs=2;
   file '.\class.csv';
   input;
   put _infile_;
run;

DATA _NULL_ with a PUT statement

Sample program, use of FILE statement LRECL= may be necessary.

Data _null_;   
   file '.\class.csv' dsd dlm=',';
   set sashelp.class ;
   put (_all_) (+0);
run;

DATA _NULL_ with a PUT statement, all fields quoted

This example uses the ~ format modifier to quote all the fields in the CSV file.
Sample program

Data _null_;   
   file '.\class.csv' dsd dlm=',';
   set sashelp.class ;
   put ( _all_ ) (~);
run;

ODS CSV and PROC REPORT with suppressed column headers

To create CSV files (i.e., this technique won't work for other types of delimited files), ODS CSV can be used with PROC REPORT. The '  ' in the DEFINE statement specifies that no column header text is to be included. Since none of the columns have header text, the header row is suppressed.
Sample program

ods csv file = '\class.csv';
proc report data = sashelp.class nowd;
 define _all_ / display ' ';
run;
ods csv close;

The CSV tagset and the table_headers="NO" option

Sample program by Richard A. DeVenezia
%let output_folder = %sysfunc(pathname(WORK));
 
ods listing close;
ods tagsets.csv 
  file="&output_folder.\class-noheader.csv"
  options(table_headers="no")
;
 
proc print noobs data=sashelp.class;
  where name < 'J';
run;
 
ods tagsets.csv close;
ods listing;
 
Direct Link: http://www.sascommunity.org/wiki/Create_a_CSV_file_without_column_names/headers_in_row_1 
 
ShareThis