SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Hierarchical Files
Creating One Observation per Detail Record


Retaining Variable Values

As you write the DATA step to read this file, remember that you want to keep the header record as a part of each observation until the next header record is encountered. To do this, you need to use a RETAIN statement to retain the values for Address across iterations of the DATA step.


General form, RETAIN statement:
RETAIN <variable(s)>;

where variable(s) names a variable whose value is retained across iterations of the DATA step.

You can specify more than one variable in a RETAIN statement.

RETAIN variable1 variable2;

If you do not specify a variable in the RETAIN statement, then the values of all the variables in the data set are retained.

RETAIN;


Caution: When a RETAIN statement specifies variables, new variables are created. Therefore, you must name any variables used in a RETAIN statement exactly as you want them stored in the data set.



     data perm.people;
        infile census;
        retain Address;   
1---+----10---+----
321 S. MAIN ST 
P MARY E    21 F
P WILLIAM M 23 M 
P SUSAN K    3 F


Next, you need to read the first field in each record, which identifies the record's type. You also need to use the @ line-hold specifier to hold the current record so that the other values in the record can be read.



     data perm.people;
        infile census;
        retain Address;
        input type $1. @;    
V---+----10---+----
H 321 S. MAIN ST 
P MARY E    21 F
P WILLIAM M 23 M 
P SUSAN K    3 F


back||next


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

Terms of Use & Legal Information | Privacy Statement