1
votes

I would like to read a number of .csv files into a single SAS dataset using a pattern match. For example if in the directory /home/datasets there are 5 files:

/home/datasets
   ~/output_group1a.csv
   ~/output_group1b.csv
   ~/output_group1c.csv
   ~/output_group2a.csv
   ~/output_group2b.csv

All with known and identical structures and data types. I would like to read in only those files corresponding to group 1 without having to explicitly specify the filenames.

1
@RobertPenridge I do not want them merged, I would like the files stackedmlegge
It's just a poor choice of words in the title... The linked question does answer your question.Robert Penridge
I think it's a duplicate, but I also don't like the answers for that one as much as the answer below - this one handles things like 1st obs.Joe
Actually, @RobertPenridge, that question does want to merge (side by side, not set) the data - note that it has an ID variable and a Different second variable for each file.Joe

1 Answers

4
votes

You can use a wildcard in your infile statement. If you have headers in each file you'll need to account for that. Here's a bit more of an example.

https://gist.github.com/statgeek/4c27ea9a7ed6d3528835

data try01;

length filename txt_file_name $256;

retain txt_file_name;

infile "Path\*.txt" eov=eov filename=filename truncover;

input@;

if _n_ eq 1 or eov then do;

txt_file_name = scan(filename, -2, ".\");

eov=0;

end;

else input

 *Place input code here;

;



run;