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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.