This is part two of an ongoing series into how to accomplish a task WITHOUT using a macro.
In the previous post, I split a file without using a macro. In this post, I go over how
read in text files that have the same format and create a single file.
One method of doing so is:
Instead of this method, SAS provides a wildcard method of referring to files.
This means if my file reference looks like:
SAS will read all CSV folders in the folder myfolders.
You can limit your wild card as well using a more specific naming convention
For example, the follow line of code will import all files that start with the word cars:
However there are some things to consider:
Fortunately SAS also provides options for those, EOV allows us to identify the first
record in a file and FILENAME allows us to identify the file name of the file imported.
Enough talking, here's what the full code would look like. Hope you find it useful!
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;
This code is also available on GitHub