BookmarkSubscribeRSS Feed
ericykc
Fluorite | Level 6

It consists of a header record for a household and isimmediately followed by one record (row) for each household member, if applicable. For
example, a household of three members will have three records exactly after its headerrecord. A household may have no identified member at the time of the survey. In that case,the household will have only a header record in the file.
May I ask that how to let the programme know how many record under the Header? Thank a lot!

 

A1234567BC012,A
15/FEB/1980,Y,Male,Married,3,FT,55000
3/JUN/1982,N,Female,Married,3,UE,0
24/JAN/2005,N,Male,Unknown,2,NA,0
D135EG023456789,B
19/OCT/1950,Y,Female,Divorced,0,PT,5000
X123A567F9,A
B2345234CC,A
21/MAY/1975,N,Male,Married,2,FT,30000
30/JUN/1978,Y,Female,Married,1,PT,10000

1 REPLY 1
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 924 views
  • 0 likes
  • 2 in conversation