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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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