The idea here is to read in the HHID and HHTYPE, but retain those values while counting household members, until a new raw data household line is encountered.
The "trick" here is to read a line of data, but defer reading the HHID and HHTYPE variables. This is done by using the _INFILE_ automatic variable that is generated with every INPUT statement. So the "naked" INPUT statement in the loop below simply generates _INFILE_. When an _INFILE_ with only two comma-separated values is encountered, you know you've finished counting the previous HHID membership. That's when you output the exhausted household, and can identify the incoming household.
data want;
infile 'path to your hirarchical file' dlm=',' end=end_of_data ;
retain hhid ' ' hhtype ' ';
do mem_count=0 by 1 until (countw(_infile_,',')=2 or end_of_data);
input;
end;
if hhid^=' ' then output;
hhid=scan(_infile_,1,',');
hhtype=scan(_infile_,2,',');
run;
... View more