DATA Step, Macro, Functions and more

I want to read only a few lines from multiple text files

Reply
Super Contributor
Posts: 281

I want to read only a few lines from multiple text files

Suppose I have multiple text files in a directory, and I want to read only a few lines from each and then skip the rest of the text file and move on to the next text file.

So, I know I can read multiple files using an INFILE statement something like

INFILE 'c:\data\*.txt';

followed by the proper input statements.

But this reads every line in the file and only goes to the next file when an end-of-file is reached. How can I tell SAS to stop reading one file when a certain text string is found and then advance to the next file without reading each line?
Valued Guide
Posts: 2,175

Re: I want to read only a few lines from multiple text files

review the doc on the infile option FILEVAR=
Super User
Posts: 9,681

Re: I want to read only a few lines from multiple text files

Hi.
What is your condition of judgement when stop read the rest of text file and continue to read next file?
Super Contributor
Super Contributor
Posts: 3,174

Re: I want to read only a few lines from multiple text files

Consider using the DATA step and the INFILE parameters EOV= to detect the new-file start (after reading the first file). And if you need know the current file-name, use the FILENAME= parameter (after declaring a LENGTH for ).

Here's an example to read all files that start with FRED*.TXT -- the multiple PUTLOG statements demonstrates SAS behavior with the EOV= being set on the first-record instance, except for the _N_=1 condition:

data _null_;
length fileinfo $255;
infile 'c:\temp\fred*.txt' end=eof eov=eov filename=fileinfo;
input @;
putlog '>diag-before>' / _all_;
if eov=1 then do;
recnum=0;
eov=0;
end;
recnum+1;
putlog '>diag-after>' / _all_;
run;

Review the SAS 9.2 INFILE statement parameter documentation for more details.


Scott Barry
SBBWorks, Inc.
Super Contributor
Posts: 281

Re: I want to read only a few lines from multiple text files

Thanks, Scott, I received a different reply, but this looks interesting as well, and I will test it out.
Valued Guide
Posts: 2,175

Re: I want to read only a few lines from multiple text files

when comparing the effect of * in the filename in the infile statement with use of filevar= you should find they both work but being able to switch to the next file at any stage (for example: after reading 5 lines) the FILEVAR= method can read less and so should run faster.
Ask a Question
Discussion stats
  • 5 replies
  • 204 views
  • 0 likes
  • 4 in conversation