COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

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

• Email
• Follow

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

--
Paige Miller
paige\dot\miller \at\ kodak\dot\com

 0

See related articles to this posting

Paige,

There are probably better ways to do it, but I think the following
will accomplish what you want:

test1.txt:
this is the first file, first line
this is the first file, second line
this is the first file, third line
this is the first file, fourth line

test2.txt
this is the second file, first line
this is the second file, second line
this is the second file, third line
this is the second file, fourth line

filename files pipe "dir ""c:\testit\test*.txt"" /S /B ";

Data x;
input fname $25.; infile dummy filevar=3Dfname pad truncover end=3Deof ; do until(eof); input stuff$40.;
if index(stuff,'third line') then eof=3D1;
else output;
end;
run;

HTH,
Art
------------
On Dec 1, 8:28=A0am, Paige Miller <paige.mil...@kodak.com> wrote:
> 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?
>
> --
> Paige Miller
> paige\dot\miller \at\ kodak\dot\com


 0

On Dec 1, 9:02=A0am, art297 <art...@rogers.com> wrote:

> There are probably better ways to do it, but I think the following
> will accomplish what you want:
>
> test1.txt:
> this is the first file, first line
> this is the first file, second line
> this is the first file, third line
> this is the first file, fourth line
>
> test2.txt
> this is the second file, first line
> this is the second file, second line
> this is the second file, third line
> this is the second file, fourth line
>
> filename files pipe "dir ""c:\testit\test*.txt"" /S /B ";
>
> Data x;
> =A0 infile files pad truncover;
> =A0 input fname $25.; > =A0 infile dummy filevar=3Dfname pad truncover end=3Deof ; > =A0 do until(eof); > =A0 =A0 =A0 input stuff$40.;
> =A0 =A0 =A0 if index(stuff,'third line') then eof=3D1;
> =A0 =A0 =A0 else output;
> =A0 end;
> run;
>
> HTH,
> Art

Art,

Thanks! That's the solution. Why didn't I think of that?

--
Paige Miller
paige\dot\miller \at\ kodak\dot\com

 0

2 Replies
444 Views

Similar Articles

12/1/2013 2:51:31 PM
page loaded in 442603 ms -1