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
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;
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:
Thank you 🙂
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.