SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Hierarchical Files
Creating One Observation per Header Record


Determining the End of the External File

Your program writes an observation to the data set only when another header record is read and the DATA step has executed more than once. But after the last detail record is read, there are no more header records to cause the last observation to be written to the data set.



     data perm.residnts;
        infile census;
        retain Address;
        input type $1. @;
        if type='H' then do;
           if _n_ > 1 then 
output;
Total=0; input Address $ 3-17; end; else if type='P'
then total+1;
   
1---+----10---+----20
H 321 S. MAIN ST 
P MARY E    21 F
P WILLIAM M 23 M 
P SUSAN K    3 F
H 324 S. MAIN ST 
P THOMAS H  79 M
P WALTER S  46 M
P ALICE A   42 F 
P MARYANN A 20 F 
P JOHN S    16 M 
H 325A S. MAIN ST
P JAMES L 34 M 
P LIZA A 31 F 
H 325B S. MAIN ST
P MARGO K 27 F 
P WILLIAM R 27 M 
P ROBERT W 1 M 


You need to determine when the last record in the file is read so that you can then execute another explicit OUTPUT statement. You can determine when the current record is the last record in an external file by specifying the END= option in the INFILE statement.


General form, INFILE statement with the END= option:
INFILE file-specification END=variable;

where variable is a temporary numeric variable whose value is 0 until the last line is read and 1 after the last line is read.

Like automatic variables, the END= variable is not written to the data set.


In the following example, the END= variable is defined in the INFILE statement as Last. When Last has a value other than 0, the OUTPUT statement writes the last observation to the data set.
     data perm.residnts;
        infile census end=last;
        retain Address;
        input type $1. @;
        if type='H' then do;
           if _n_ > 1 then output;
           Total=0;
           input Address $ 3-17;
        end;
        else if type='P' then total+1;
        if last then output;

A DROP= option in the DATA statement drops the variable type from the data set, and a RUN statement completes the DATA step.

     data perm.residnts (drop=type);
        infile census end=last;
        retain Address;
        input type $1. @;
        if type='H' then do;
           if _n_ > 1 then output;
           Total=0;
           input Address $ 3-17;
        end;
        else if type='P' then total+1;
        if last then output;
     run;

back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement