Reading Hierarchical Files |
Creating One Observation per Header Record |
Reading Detail Records
You need to define an alternative action when the value of |
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 |
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 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; |
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; |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.