BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sean_OConnor
Obsidian | Level 7

Folks,

 

I have a dataset of individuals which I would like to assign to households. I've noticed in my data that in some cases certain individuals have been assigned to to two different houses.

 

Thus, I would like to tell SAS to overwrite this second household id with the first instance. However, when I run my code, these second values are replaced with a 1. Any advice would be most welcome.

 

 

Here is my data at the start;

 

Person_IdRelation_IdHouse_Id
01000N035608N42D00
01000N1011E4D2106

 

Here is what I want;

 

 

Person_IdRelation_IdHouse_Id
01000N035608N42D00
01000N1011E42D00

 

 

This is what happens when I run my code;

 

Person_IdRelation_IdHouse_Id
01000N035608N42D00
01000N1011E1

 

 

proc sort data=households_2013 out=test; by Person_id house_id;run;
data trial;
set test;
by person_id house_id;
if not first.person_id and last.person_id then house_id= first.house_id;run;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Th first. and last. variables automatically created by the use of a by statement are boolean variables and can only take the values 0 (false) or 1 (true).

 

You will need to keep the house_id in a retained variable:

proc sort
  data=households_2013
  out=test
;
by person_id house_id;
run;

data trial;
set test;
by person_id;
retain save_house;
if first.person_id
then save_house = house_id;
else house_id = save_house;
drop save_house;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Th first. and last. variables automatically created by the use of a by statement are boolean variables and can only take the values 0 (false) or 1 (true).

 

You will need to keep the house_id in a retained variable:

proc sort
  data=households_2013
  out=test
;
by person_id house_id;
run;

data trial;
set test;
by person_id;
retain save_house;
if first.person_id
then save_house = house_id;
else house_id = save_house;
drop save_house;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

You are nearly there:

proc sort data=households_2013 out=test;
  by person_id house_id;
run;

data trial;
  set test;
  by person_id house_id;
  retain new_house_id;
  if first.person_id then new_house_id=house_id;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 2 replies
  • 1369 views
  • 0 likes
  • 3 in conversation