Reading Hierarchical Files |
Lesson
Summary![]() ![]() |
This page contains
|
I. Text Summary
To go to the page where a task, programming feature, or concept was presented, select a link. |
Hierarchical Raw Data Files Raw data files can be hierarchical in structure, consisting of a header record and one or more detail records. You can build a SAS data set from a hierarchical file by creating one observation
|
Creating an Observation per Detail Record In order to create one observation per detail record, it is necessary to distinguish between header and detail records. Having a field that identifies the type of the record makes this task easier. As you write the DATA step, use a RETAIN statement to keep the header record as a part of each observation until the next header record is encountered. Next, you need to read the field in each record that identifies the record's type. Remember to use the @ line-hold specifier to hold the current record so that the other values in the record can be read. Use an IF-THEN statement to check for the condition that the record is a header record. If the record is a header record, you need to execute an INPUT statement to read the variable values for that record. You can use a subsetting IF statement to check for the condition that the record is a detail record. If the record is a detail record, use another INPUT statement to read the variable values in that record. You can use the use the DROP= option to drop the variable that identifies each record's type from the data set. |
Creating an Observation per Header Record Creating one observation per header record condenses a large amount of information into a concise data set. As you write the DATA step, you need to think about performing several tasks. As with creating one observation per detail record, use a RETAIN statement to keep the header record as a part of each observation until the next header record is encountered. Then read the field in each record that identifies the record's type. Remember to use the @ line-hold specifier to hold the current record so that the other values in the record can be read. When the record is a header record, multiple statements need to be executed. You can do this by adding a simple DO group to an IF-THEN statement. Within the DO group, you need to
When the record is a detail record, you need to define an alternative set of actions. You can do this by adding an ELSE statement to an IF-THEN statement. As each detail record is read, you can increment the value of the summary variable by using a sum statement. 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. You can determine when the current record is the last record in an external file by specifying the END= option in the INFILE statement. Again, you can use the use the DROP= option to drop the variable that identifies each record's type from the data set.
|
II. Syntax
To go to the page where a statement or option was presented, select a link. Syntax to Create One Observation for Each Detail Record |
LIBNAME libref 'SAS-data-library'; |
FILENAME fileref 'filename'; |
DATA=SAS-data-set (DROP= variable); |
INFILE raw-data-file; |
RETAIN variable; |
INPUT variable; |
|
IF variable='condition'; |
SAS-statement; |
RUN; |
Syntax to Create One Observation for Each Header Record |
LIBNAME libref 'SAS-data-library'; |
FILENAME fileref 'filename'; |
DATA=SAS-data-set (DROP= variable); |
INFILE raw-data-file END=variable; |
RETAIN variable; |
INPUT variable; |
IF variable='condition' THEN DO; |
IF _N_ > 1 THEN OUTPUT; |
summary-variable=0; |
INPUT variable; |
END; |
|
IF variable THEN OUTPUT; |
RUN; |
|
III. Sample Programs
Program to Create One Observation for Each Detail Record |
libname perm 'c:\records\census2k'; filename census 'c:\records\census2k\survey.dat'; 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; |
Program to Create One Observation for Each Header Record |
libname perm 'c:\records\census2k'; filename census 'c:\records\census2k\survey.dat'; 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; |
IV. Points to Remember
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.