mlegge
mlegge

Reputation: 6913

Read in Files with pattern match into one SAS dataset

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.

Upvotes: 1

Views: 605

Answers (1)

Reeza
Reeza

Reputation: 21264

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;

Upvotes: 4

Related Questions