Let's say that we have multiple raw data files in a folder with the same data structure and we need to read them into SAS to form a single SAS data set. This can actually be done in SAS in a single data step. Here is an example demonstrating the steps to accomplish that for Windows operating system environment. There are mainly two data steps. Step one is to create a data file consisting of all the file names. Step two is to create a single data file combining all the data read from the files named in the first step.
For the example, we assume that we are working on a Windows machine and that we are going to read in all the text data files with .txt extension in a folder.
%let dirname = c:\work\raw_data_files; filename DIRLIST pipe "dir /B &dirname\*.txt"; data dirlist ; length fname $256; infile dirlist length=reclen ; input fname $varying256. reclen ; run; proc print data = dirlist; run;
Obs fname 1 file01.txt 2 file3.txt 3 file7.txt
data all_text (drop=fname); length myfilename $100; length name $25; set dirlist; filepath = "&dirname\"||fname; infile dummy filevar = filepath length=reclen end=done missover; do while(not done); myfilename = filepath; input name $ x1 x2 x3; output; end; run;proc print data=all_text; run;Obs myfilename name x1 x2 x31 C:\work\raw_data_files\file01.txt John 12 354 7 2 C:\work\raw_data_files\file01.txt Carl 43 657 9 3 C:\work\raw_data_files\file01.txt Mary 343 7 9 4 C:\work\raw_data_files\file3.txt adam 12 354 7 5 C:\work\raw_data_files\file3.txt brad 43 657 9 6 C:\work\raw_data_files\file3.txt tyler 343 7 9 7 C:\work\raw_data_files\file7.txt mary 343 56 2 8 C:\work\raw_data_files\file7.txt robert 243 67 8 9 C:\work\raw_data_files\file7.txt brad 43 657 9 10 C:\work\raw_data_files\file7.txt tyler 343 7 9
The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.