/* Three ways to read excel files into SAS */ /* the default format is assumed to be: /* row 1 = variable names, /* subsequent rows = observations, /* columns = variables */ /* 1) from a .xls file */ proc import datafile='c:/philip/stat 402/camp.xls' out=camp replace; run; /* replace tells SAS to reread the file if the data set already */ /* exists. This is necessary if you've corrected the data file */ /* additional options (place before the ;) are: sheet = 'name' to read a specific sheet from a workbook not needed if the sheet is named sheet1 (the excel default) getnames = no when the first row is data, not variable names */ /* the file must NOT be open in excel, otherwise SAS /* refuses to read the file */ proc print data=camp(obs=10); title 'Importing a .xls file'; run; /*2) from a .csv copy of a specific sheet */ proc import datafile='c:/philip/stat 402/camp.csv' out=camp replace; /* the file must NOT be open in excel, otherwise SAS /* refuses to read the file */ proc print data=camp(obs=10); title 'Importing a .csv file '; run; /* if you want to do any transformations, computations, /* or recoding, then you need another data step */ data camp2; set camp; loghr0 = log(hr0); loghr4 = log(hr4); /*3) using a data step and input statement to read a .csv file */ data camp; infile 'c:/philip/stat 402/camp.csv' dsd firstobs=2; input .....; /* put additional data step commands here */ /* you need to provide the variable names in the input command */ /* the dsd option to the infile is crucial */ /* it tells SAS to deal with commas and quotes in the appropriate /* way to correctly read a .csv file */ /* the firstobs=2 tells SAS to skip the first line */ /* appropriate only if the first line is column names */ proc print data=camp(obs=10); title 'Using a data step'; run; /*4) reading a tab-delimited file /* (with tab characters instead of spaces between values ) */ data camp; infile 'c:/philip/stat 402/camp.prn' dlm='09'x; input .....; /* the dlm= changes the delimiter, the separator between */ /* fields. The '09'x is the tab character in hexadecimal code */ /* And finally, how to save a SAS data set as an excel file */ /* imagine that SAS has created a data set called means */ proc export data=means outfile='c:\philip\stat 402\means.xls'; /* to create an excel workbook */ run; proc export data=means outfile='c:\philip\stat 402\means.csv'; /* to create a .csv file */ run;