Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append multiple CSV files in SAS

Tags:

csv

sas

I need to import a large amount of csv files in one SAS dataset. They all have the same data structure (same variables, variable names on the first line). I usually work in SQL, but I am forced to this particular project in SAS of which I have only basic knowledge.

For the moment, my code looks like this:

proc import out=work.data
   datafile = file1.csv
   DBMS=CSV REPLACE;
   GETNAMES=YES;
   DATAROW=2;

proc import out=work.newData
   datafile = file2.csv
   DBMS=CSV REPLACE;
   GETNAMES=YES;
   DATAROW=2;

proc append base=work.data 
            data=work.newData force;    
run;

and so on for file3.csv ... file4.csv.

There is, I am sure, a more elegant way of doing this, that is, looping over all csv files on one folder without writing them explicitly (there are a few thousand files).

Thanks for your help.

like image 821
user2816263 Avatar asked Sep 25 '13 17:09

user2816263


1 Answers

You need to figure out the input statement, rather than using PROC IMPORT (though if you use PROC IMPORT once, it will politely write that input code to the log which you can then use), and then you can use wildcards:

data mydata;
infile "c:\temp\*.csv" dlm=',' missover lrecl=32767;
input
myvar1
myvar2 $
myvar3
myvar4 :date9.
;
run;

Some other options exist; see https://communities.sas.com/message/182012#182012 for example for other ways to do it.

like image 143
Joe Avatar answered Nov 15 '22 09:11

Joe