DATA Step, Macro, Functions and more

Use FIRSTOBS option in INFILE statement reading multiple files

Reply
N/A
Posts: 0

Use FIRSTOBS option in INFILE statement reading multiple files

Hi,
I need to read many text files from a directory into a one SAS data set.
All the text files have the same varibles but different number of observation.
I've tried to use a wildcard into the INFILE and the program works ok:

data pippo;
INFILE "C:\tmp\*.txt"
DLM=';' DSD missover FIRSTOBS=2;
input A
B
C
D: $4.
E: $9.;
run;

The problem is that i have to read just since the second observation of every text file (becouse the first one are the titles), and if i use the FIRSTOBS option in the INFILE, it applies just for the first text file, but in the other one it reads all the obs.
Do you know how i could apply the FIRSTOBS =2 in every text file?

Thanks,
Elena
Respected Advisor
Posts: 3,777

Re: Use FIRSTOBS option in INFILE statement reading multiple files

When reading with a wild card the files are treated as one stream. There is an option EOV to detect the start of a new file. You could test that variable and use programming logic to skip the first line of the file.

You CAN use FIRSTOBS when reading the files with the FILEVAR option. This requires creating a list of files to be read.

You should consult the documentation regarding the details of these INFILE statement options.
N/A
Posts: 0

Re: Use FIRSTOBS option in INFILE statement reading multiple files

With the help of the option EOV i have got solve the problem

Thank you!
Contributor
Posts: 29

Re: Use FIRSTOBS option in INFILE statement reading multiple files

I would be curious to see the result. The way I understand EOV is that it changes from 0 to 1 as soon as it encounters a new file. So reading in more than two files the EOV variable cannot tell you when you start reading in the third file. Or am I wrong?
Valued Guide
Posts: 2,174

Re: Use FIRSTOBS option in INFILE statement reading multiple files

niemannk

you're wrong

EOV is set when "reading after last record" occurs on any file and there is more data to follow, through "*" or file concatenation. So the EOV= flag is set after all files except the last.


PeterC
Contributor
Posts: 29

Re: Use FIRSTOBS option in INFILE statement reading multiple files

ok. I played around with it and it looks like i need to reset the eov variable. sort of like this:

if _n_=1 or eov then do;
[...];
eov=0;
end;
Respected Advisor
Posts: 3,777

Re: Use FIRSTOBS option in INFILE statement reading multiple files

The documentation is pretty clear on this one.

[pre]
EOV=variable
names a variable that SAS sets to 1 when the first record in a file in a series of
concatenated files is read. The variable is set only after SAS encounters the next
file. Like automatic variables, the EOV= variable is not written to the data set.

Tip: Reset the EOV= variable back to 0 after SAS encounters each boundary.
[/pre]
Ask a Question
Discussion stats
  • 6 replies
  • 1460 views
  • 0 likes
  • 4 in conversation