SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Hierarchical Files
Creating One Observation per Header Record


Reading Detail Records

You need to define an alternative action when the value of type is not H. You can do this by adding an ELSE statement to the IF-THEN statement.

Remember that the IF-THEN statement executes a SAS statement when the condition specified in the IF clause is true. Adding an optional ELSE statement defines an alternative action when the THEN clause is not executed. If used, the ELSE statement must immediately follow the IF-THEN statement.
     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

The only other type of record is a detail record, represented by a P. You want to count each person represented by a detail record and store the accumulated value in the summary variable Total. You do not need to read the values for Name, Age, and Gender.


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 


     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

You have already initialized the value of Total to 0 each time that a header record is read. Now as each detail record is read, you can increment the value of Total by using a sum statement. In this example you're counting the number of detail records for each header record, so you'll increment the value of  Total by 1 when the value of type is P.

     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;

Caution: Remember that a sum statement enables you to add any valid SAS expression to an accumulator variable. Depending on your data, you may need to add the value of another variable to the accumulator variable.
     else if type='B' then total+cost;


back||next


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

Terms of Use & Legal Information | Privacy Statement