BookmarkSubscribeRSS Feed
fengyuwuzu
Pyrite | Level 9

I have 637 folders, which contain 12,256 csv files. each folder may have different mumber of csv files.

each file is for one person. They all have the same header columns in all csv files.

 

I can important one file correctly. so based on that, I may be able to import and append all of them into one sungle file.

Can anyone kindly give me a template how to set up the loops to loop through all the folders and all the files in each folder?

 

Thank you very much

 

7 REPLIES 7
fengyuwuzu
Pyrite | Level 9

I read that post recently. Actually I used it successully when reading files in the same folder.

 

Now my task is more difficult, because I need to loop the folders as well.

Reeza
Super User

I updated the answer to account for this. If you have issues, post your full code and error. 

fengyuwuzu
Pyrite | Level 9
I willl give it a try and see how it goes.

Thank you very much!!
Reeza
Super User

You'll need some modifications to the macro I linked to.

 

1. Get a list of files into a dataset, use the OS to list files.  There's quite a few samples on here so a short search should pop up something. 

2. Create a reference to a file - dummy file not used.

 

file my_file 'myfile.txt';

3. SET the data set from #1 into the data set

 

4. Modify the infile statement to use the variable from #1 that contains the file path using the filevar option.

 

infile my_file eov=eov filevar=myfile filename=filename truncover;
data_null__
Jade | Level 19

How about a combination of FILEVAR and wildcard?  These folder names are UNIX.  I forget your OS.  You can use programming (not shown) to create your 600 or so folder paths.

 

 


data folders;
   input folder $80.;
   cards;
~/folder01
~/folder02
;;;;
   run;
data _null_;
   set folders;
   length filename filevar $128;
   filevar=catx('/',folder,'*.csv');
   put filevar=;
   infile dummy filevar=filevar filename=filename eov=eov end=eof;
   do _n_ = 1 by 1 while(not eof);
      input @;
      if _n_ eq 1 or eof then do;
         put 'NOTE: ' filename= eov=;
         end;
      input;
      end;
   run;

 

sas-innovate-2024.png

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.

 

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
  • 7 replies
  • 3685 views
  • 0 likes
  • 3 in conversation