Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active May 8, 2019 15:01
Show Gist options
  • Save statgeek/4c27ea9a7ed6d3528835 to your computer and use it in GitHub Desktop.
Save statgeek/4c27ea9a7ed6d3528835 to your computer and use it in GitHub Desktop.
SAS - read in multiple text files using wild card and get the filename
data import_all;
*make sure variables to store file name are long enough;
length filename txt_file_name $256;
*keep file name from record to record;
retain txt_file_name;
*Use wildcard in input;
infile "Path\*.txt" eov=eov filename=filename truncover;
*Input first record and hold line;
input@;
*Check if this is the first record or the first record in a new file;
*If it is, replace the filename with the new file name and move to next line;
if _n_ eq 1 or eov then do;
txt_file_name = scan(filename, -1, "\");
eov=0;
end;
*Otherwise go to the import step and read the files;
else input
*Place input code here;
;
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment