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
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?
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:
length fileinfo $255;
infile 'c:\temp\fred*.txt' end=eof eov=eov filename=fileinfo;
putlog '>diag-before>' / _all_;
if eov=1 then do;
putlog '>diag-after>' / _all_;
Review the SAS 9.2 INFILE statement parameter documentation for more details.
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.