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

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 931 views
  • 0 likes
  • 3 in conversation