Appending csv files in SAS
I have a bunch of csv files. Each has data from a different period:
filename file1 'JAN2011_PRICE.csv';
filename file2 'FEB2011_PRICE.csv';
...
Do I need to manually create intermediate datasets and then append them all together? Is there a better way of doing this?
SOLUTION
F开发者_JS百科rom the documentation it is preferable to use:
data allcsv;
length fileloc myinfile $ 300;
input fileloc $ ; /* read instream data */
/* The INFILE statement closes the current file
and opens a new one if FILELOC changes value
when INFILE executes */
infile my filevar=fileloc
filename=myinfile end=done dlm=',';
/* DONE set to 1 when last input record read */
do while(not done);
/* Read all input records from the currently */
/* opened input file */
input col1 col2 col3 ...;
output;
end;
put 'Finished reading ' myinfile=;
datalines;
path-to-file1
path-to-file2
...
run;
To read a bunch of csv files into a single SAS dataset, you can use a single data step as described in the SAS documentation here. You want the second example in this section which uses the filevar=
infile option.
There should be no reason to create intermediate datasets.
The easiest method is to use a wildcard.
filename allfiles '*PRICE.csv';
data allcsv;
infile allfiles end=done dlm=',';
input col1 col2 col3 ...;
run;
精彩评论