BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sreelekha
Fluorite | Level 6

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
1 ACCEPTED SOLUTION

Accepted Solutions
srinath3111
Quartz | Level 8

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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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:

 

Sreelekha
Fluorite | Level 6

Thank you 🙂

srinath3111
Quartz | Level 8

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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