SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Hierarchical Files
Creating One Observation per Detail Record


Reading a Detail Record

Now think about what needs to happen when a detail record is read. Remember, you want to write an observation to the data set only when the value of type is P.


V---+----10---+----
H 321 S. MAIN ST 
P MARY E    21 F
P WILLIAM M 23 M 
P SUSAN K    3 F


You can use a subsetting IF statement to check for the condition that type is P. The remaining DATA step statements execute only when the condition is true. If type is not P, then the values for Name, Age, and Gender are not read, the values in the program data vector are not written to the data set as an observation, and control returns to the top of the DATA step.

The values for Address are retained only as a part of the observation. Remember, you do not want to create an observation for each header record.

     data perm.people;
        infile census;
        retain Address;
        input type $1. @;
        if type='H' then input @3 Address $15.;
        if type='P';
        input @3 Name $10. @13 Age 3. @15 Gender $1.;
     run;  

Dropping Variables

Because type is useful only for identifying a record's type, drop the variable from the data set. The DROP= option in the DATA statement shown here prevents the values of a variable from being written to the data set.

      data perm.people (drop=type);  
         infile census;
         retain Address;
         input type $1. @;
         if type='H' then input @3 Address $15.;
         if type='P';
         input @3 Name $10. @13 Age 3. @15 Gender $1.;
      run;

back||next


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

Terms of Use & Legal Information | Privacy Statement