BookmarkSubscribeRSS Feed
Paige
Quartz | Level 8
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?
5 REPLIES 5
Peter_C
Rhodochrosite | Level 12
review the doc on the infile option FILEVAR=
Ksharp
Super User
Hi.
What is your condition of judgement when stop read the rest of text file and continue to read next file?
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Paige
Quartz | Level 8
Thanks, Scott, I received a different reply, but this looks interesting as well, and I will test it out.
Peter_C
Rhodochrosite | Level 12
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.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2008 views
  • 0 likes
  • 4 in conversation