/*** TIP00394 ***/
Firstobs option ignored with multiple file read

I am using a wildcard in the infile statement to read a
number of text files. The first line of each text file lists the column
names, and I want to skip over these. In principal, the 'firstobs='
option in the infile statement would accomplish this. However, SAS v8.2
only respects the 'firstobs=' option for the first file read, and
ignores it for each subsequent file. The result is that SAS reads
the first line of each text file after the first, resulting in invalid
data errors for each of the numeric variables (since it is trying
to read column names as numbers!).


        The code is shown below (note the '*' wildcard in the file
        specification):

        Data WORK.X;
           infile "E:\Users\Lymphoma - *.txt"
        delimiter='09'x MISSOVER DSD lrecl=32767 firstobs = 2;

        informat No_hospital $2. ;
        informat No_Patient best32. ;

        format No_hospital $2. ;
        format No_Patient best12. ;

        input
             No_hopital $
             No_Patient
        ;
        output;
        run;

Solution #1 by Droogendyk, Harry A change in methodology. Rather than specifying the wildcard on the INFILE statement, pipe the results of the DOS DIR command. Use a second INFILE statement with the FILEVAR= option to read each file in turn. Note that the firstobs specification is respected for each file read. Test data and output included: 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:\temp\test*.txt"" /S /B "; Data x; infile files pad truncover; input fname $25.; infile dummy filevar=fname pad truncover firstobs = 2 end=eof ; do until(eof); input stuff $40.; output; end; run; proc print; run; Obs stuff 1 this is the first file, second line 2 this is the first file, third line 3 this is the first file, fourth line 4 this is the second file, second line 5 this is the second file, third line 6 this is the second file, fourth line
Solution #2 by Ian Whitlock You could skip the first lines yourself. Take a look at filename q1 "c:\junk\junkq1.txt" ; filename q2 "c:\junk\junkq2.txt" ; data _null_ ; file q1 ; put "xxxxxxxx" ; do x = 1 to 5 ; put x ; end ; file q2 ; put "xxxxxxxx" ; do x = 1 to 5 ; put x ; end ; run ; filename q "c:\junk\junkq*.txt" ; data w ; retain prevfn ; length fn prevfn $ 50 ; infile q filename = fn ; input @ ; if fn ^= prevfn then do ; input ; prevfn = fn ; end ; input x ; run ; Or more generally, %let firstobs = 3 ; data w ( drop = prevfn cnt ) ; retain prevfn ; length fn prevfn $ 50 ; infile q filename = fn ; input @ ; if fn ^= prevfn then do ; do cnt = 1 to &firstobs - 1 ; input ; end ; prevfn = fn ; end ; input x ; run ;
/*** end of tip 00394 ***/