DATA Step, Macro, Functions and more

Importing a a .csv file to a SAS dataset

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Importing a a .csv file to a SAS dataset

Hi All,

 

My requirement is to import data from a number of .csv files into a SAS data set.

 I am using the following code to do the same. The .csv files ( 5 in number namely DATROCF1,DATROCF2,DATROCF3,DATROCF4,DATROCF5) ) have been placed in the shared path. The issue which I am facing is that. the dataset 'DATRDOCF' has data only from the 5th file in the path. and not from all the files.

Can you please help me out.

%let subdir=F:\pol\New folder\;
filename dir "&subdir.*.csv ";
data new;
length filename fname $ 200;
infile dir eof=last filename=fname;
input ;
last: filename=fname;
run;
proc sort data=new nodupkey;
by filename;
run;
data null;
set new;
call symputx(cats('filename',_n_),filename);
call symputx(cats('dsn',_n_),compress(scan(filename,-2,'\.'), ,'ka'));
call symputx('nobs',_n_);
run;
%put &nobs.;
%macro import;
%do i=1 %to &nobs;
proc import datafile="&&filename&i" out=&&dsn&i dbms=csv ;
getnames=yes;
run;
%end;
%mend import;
 
%import

Accepted Solutions
Solution
‎02-13-2018 07:22 AM
Contributor
Posts: 40

Re: Importing a a .csv file to a SAS dataset

Posted in reply to Sreelekha

Hi,

 

you can try

 

 

 

%let file=DATROCF;

%put &file;

options mprint;

%macro  mac;

%do i=1 %to 5;

proc import datafile="path\&&file.&i..csv" out=test&i

dbms=csv

replace ;

/*sheet="sheet1";*/

run;

%end;

%mend;

%mac;

View solution in original post


All Replies
Super User
Super User
Posts: 9,813

Re: Importing a a .csv file to a SAS dataset

Posted in reply to Sreelekha

Use the search functionality on here or google to search for "import multiple csv files", you will find hundreds of posts.  Firstly you do not need all that code as:

data want;
  infile "f:/pol/newfolder/*.csv";
 ...
run;

Is the way to import multiple csv files.  Avoid putting them in subfolders and this will make your life easier.  Also note to avoid spaces and special characters in path names.

If you want to continue your route then you will need to post full code and log as datrdocf is not shown in this code, only: 

=&&dsn&i

I assume that you are overwriting datrdocf each time which is why you only end up with the last one, so what you need to do is to append the newly imported data to the previous lot each time except for the first iteration maybe something like:

 

Occasional Contributor
Posts: 12

Re: Importing a a .csv file to a SAS dataset

Thank you Smiley Happy

Solution
‎02-13-2018 07:22 AM
Contributor
Posts: 40

Re: Importing a a .csv file to a SAS dataset

Posted in reply to Sreelekha

Hi,

 

you can try

 

 

 

%let file=DATROCF;

%put &file;

options mprint;

%macro  mac;

%do i=1 %to 5;

proc import datafile="path\&&file.&i..csv" out=test&i

dbms=csv

replace ;

/*sheet="sheet1";*/

run;

%end;

%mend;

%mac;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 219 views
  • 1 like
  • 3 in conversation