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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 4140 views
  • 0 likes
  • 3 in conversation