Reading Hierarchical Files |
Creating One Observation per Header Record |
Determining the End of the External File
Your program writes an observation to the data set only when another header record is read and the DATA step has executed more than once. But after the last detail record is read, there are no more header records to cause the last 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 |
|
You need to determine when the last record in the file is read so that you can then execute another explicit OUTPUT statement. You can determine when the current record is the last record in an external file by specifying the END= option in the INFILE statement. |
General form, INFILE statement with the END= option:
where variable is a temporary numeric variable whose value is 0 until the last line is read and 1 after the last line is read. Like automatic variables, the END= variable is not written to the data set. |
In the following example, the END= variable is defined in the INFILE
statement as Last . When Last has a value other
than 0, the OUTPUT statement writes the last observation to the
data set.
data perm.residnts; infile census end=last; 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; if last then output;
A DROP= option in the DATA statement drops the variable data perm.residnts (drop=type); infile census end=last; 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; if last then output; run; |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.