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!
*make sure variables to store file name are long enough;
length filename txt_file_name $256;
*keep file name from record to record;
*Use wildcard in input;
infile "Path\*.txt" eov=eov filename=filename truncover;
*Input first record and hold line;
*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, "\");
*Otherwise go to the import step and read the files;
*Place input code here;
This code is also available on GitHub
The early bird rate has been extended! Register by March 18 for just $695 - $100 off the standard rate.
Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.