Desktop productivity for business analysts and programmers

Proc Import multiple CSV files

Reply
Occasional Contributor
Posts: 6

Proc Import multiple CSV files

Dear experts,

 

Me, the newbe, has created a macro to read in some csv files. It works but I think the code can be smarter since now I do this manually for something like 30 files which are all located in the same folder. Here an example for reading in 5 files.

 

the code in SAS EG:

%macro imp(infile);

PROC IMPORT OUT=SAS_TMP.&infile

datafile = "\\DWCPMCP\Data\01_INPUTDOCS\&infile..csv"

DBMS=csv REPLACE;

GETNAMES=YES;

datarow=6;

run;

%mend imp;

%imp(IL_V450)

%imp(IL_V500)

%imp(IL_V100)

%imp(IL_V350)

%imp(IL_V600)

 

My is, can I make this code less manually intensive? There are more files coming in. Can I import the whole folder in one time instead of typing them out?

 

Please advice.

 

 


CSV files.PNG
Super User
Super User
Posts: 7,727

Re: Proc Import multiple CSV files

Hi,

 

Its a good idea to do a search before posting a question.  There are literaly hundreds of posts on this topic:

https://communities.sas.com/t5/forums/searchpage/tab/message?q=import+multiple+csv+files

 

You don't need macro, you can do it in a datastep and use wildcards:

infile "<pathtocsvs>\*.csv";

All you have to remember is proper CSV files have headers, so you need to jump one row per file.

Super User
Posts: 19,193

Re: Proc Import multiple CSV files

I would recommend the methods here. You may need some modification to start at line 6. 

Note that proc import guesses types so as the number of files gets larger it's more likely that you'll get inconsistent data types, ie a VAR is char in one dataset and numeric in another. 

 

Using method above requires you explicitly specify it, once. 

Super User
Posts: 19,193

Re: Proc Import multiple CSV files

Valued Guide
Posts: 505

Re: Proc Import multiple CSV files

* create three csvs;

dm "dexport sashelp.cars'd:\csv\cars.csv' replace";
dm "dexport sashelp.class'd:\csv\class.csv' replace";
dm "dexport sashelp.classfit'd:\csv\classfit.csv' replace";

* create three sas datsets;

%symdel fyl; * just in case it exists;
data _null_;
  do csv="classfit","class","cars";
     fyl=catx(' ',"dm 'dimport",cats('"d:\csv\',csv,'.csv"'),csv,"replace';");
     call execute(fyl);
  end;
run;quit;

NOTE: WORK.CLASSFIT data set was successfully created.
NOTE: The data set WORK.CLASSFIT has 19 observations and 10 variables.

NOTE: WORK.CLASS data set was successfully created.
NOTE: The data set WORK.CLASS has 19 observations and 5 variables.

NOTE: WORK.CARS data set was successfully created.
NOTE: The data set WORK.CARS has 428 observations and 15 variables.




Ask a Question
Discussion stats
  • 4 replies
  • 1181 views
  • 2 likes
  • 4 in conversation