Ηλίας
Ηλίας

Reputation: 2640

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

From 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;

Upvotes: 2

Views: 3103

Answers (2)

Laurent de Walick
Laurent de Walick

Reputation: 2174

The easiest method is to use a wildcard.

filename allfiles '*PRICE.csv';

data allcsv;
 infile allfiles end=done dlm=',';
 input col1 col2 col3 ...;
run;

Upvotes: 2

sasfrog
sasfrog

Reputation: 2460

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.

Upvotes: 2

Related Questions