Hello
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.
data work.test2;
infile 'C:\Users\71879\temp\TEST.txt' truncover;
input //////////
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. /
;
PTfind=index(line1,'Assessed');
line1a=substr(line1,1,PTfind-1);
line1b=substr(line1,PTfind);
name=substr(line1a,8);
PATIENTS=scan(scan(line1b,2,','),1,' ');
ANT1find=index(line9,'Var2 ');
line9a=substr(line9,1,ANT1find-1);
line9b=substr(line9,ANT1find);
name1=substr(line9a,8);
Antigen1=scan(scan(line9b,2,','),1,' ');
ANT2find=index(line10,'HepB3');
line10a=substr(line10,1,ANT2find-1);
line10b=substr(line10,ANT2find);
name2=substr(line10a,8);
Antigen2=scan(scan(line10b,2,','),1,' ');
ANT3find=index(line11,'Meng1');
line11a=substr(line11,1,ANT3find-1);
line11b=substr(line11,ANT3find);
name3=substr(line11a,8);
Antigen3=scan(scan(line11b,2,','),1,' ');
ANT4find=index(line12,'MMR2');
line12a=substr(line12,1,ANT4find-1);
line12b=substr(line12,ANT4find);
name4=substr(line12a,8);
Antigen4=scan(scan(line12b,2,','),1,' ');
ANT5find=index(line13,'Var2');
line13a=substr(line13,1,ANT5find-1);
line13b=substr(line13,ANT5find);
name5=substr(line13a,8);
Antigen5=scan(scan(line13b,2,','),1,' ');
ANT6find=index(line14,'Tdap1');
line14a=substr(line14,1,ANT6find-1);
line14b=substr(line14,ANT6find);
nam6e=substr(line14a,8);
Antigen6=scan(scan(line14b,2,','),1,' ');
RUN;
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.
data _null_;
do file='one.txt','two.txt' ;
fname=catx('\',pathname('work'),file);
file out filevar=fname ;
do i=1 to 10; put i; end;
end;
run;
data want ;
length fname file $200;
infile "%sysfunc(pathname(work))\*.txt" eov=eov filename=fname;
input @;
if _n_=1 or eov then do;
input // i ;
file = scan(fname,-1,'\');
output;
end;
eov=0;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.