03-17-2015 11:32 AM
I have reports that are saved in .txt format. They are not raw data files with variable names. There are a few things I want to accomplish:
1. Extract pieces of information from these files based on where they are located. All the files have the same structure so the pieces of information I want to extract are located in the same place.
2. There is nothing in the report that designates the Facility Name. The name of the .txt file is the facility name. I want to use the name of the file as the value of a new variable, Site.
3. I want to run a code that I have created on all .txt files located in a folder directory.
4. The end sas dataset should be 1 file with X number of observations (# of files in the directory).
I have this code that works fine for number 1 on my list. However, there are potentially dozens and dozens of such files that I would need to run this code on individually, which would take a long time and won't be very efficient. How do I run this code on all .txt files in a directory at once and accomplish 2-4 on my list?
I have found pieces of code that shows me how to read in all .txt files in a directory but I don't know how to combine all the pieces into one coherent working code.
Any help would be greatly appreciated and be a big time saver for me.
This code creates variables, PATIENTS and Antigen1-Antigen6 which is what I am extracting.
infile 'C:\Users\71879\temp\TEST.txt' truncover;
line1 $100. /
line2 $100. /
line3 $100. /
line4 $100. /
line5 $100. /
line6 $100. /
line7 $100. /
line8 $100. /
line9 $100. /
line10 $100. /
line11 $100. /
line12 $100. /
line13 $100. /
line14 $100. /
03-17-2015 01:52 PM
You can use wildcards on the INFILE statement.
You will also want to use the FILENAME and EOV options on the INFILE statement.
Here is a simple example.
do file='one.txt','two.txt' ;
file out filevar=fname ;
do i=1 to 10; put i; end;
data want ;
length fname file $200;
infile "%sysfunc(pathname(work))\*.txt" eov=eov filename=fname;
if _n_=1 or eov then do;
input // i ;
file = scan(fname,-1,'\');