SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Hierarchical Files
Creating One Observation per Header Record


DO Group Actions for Header Records

When the condition type='H' is true, several statements need to be executed.

     data perm.residnts;
        infile census;
        retain Address;
        input type $1. @;
        if type='H' then do; 
  • First, you need to determine if this is the first header record in the external file. You do not want the first header record to be written as an observation until the related detail records are read and summarized.

    _N_ is an automatic variable whose value is the number of times the DATA step has begun to execute. The expression _N_ > 1 defines a condition where the DATA step has executed more than once. Use this expression in conjunction with the previous IF-THEN statement to check for these two conditions:

    1. the current record is a header record
    2. the DATA step has executed more than once.
     data perm.residnts;
        infile census;
        retain Address;
        input type $1. @;
        if type='H' then do;
           if _n_ > 1 
  • When the conditions type='H' and _n_ > 1 are true, an OUTPUT statement is executed. Thus each header record, except for the first one, causes an 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;
  • An assignment statement creates the summary variable Total and sets its value to 0.
     data perm.residnts;
        infile census; 
        retain Address; 
        input type $1. @;
        if type='H' then do;
           if _n_ > 1 then output;
           Total=0;
  • An INPUT statement reads the values for Address.
     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;
  • An END statement closes the loop.
     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; 

back||next


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

Terms of Use & Legal Information | Privacy Statement