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

Hello, Good day everyone !

 

I appreciate your help the below situation. Thanks you in advance. Phan S. 

 

data demo (drop=x);
retain id; do i= 1 to 3;
input id int age sex race gh mh;
if not missing(x) then id=x;
output;
end;
cards;
1 1 45 1 2 56 48
1 2 46 1 2 57 48
1 3 47 1 2 67 60
2 1 32 0 1 67 55
2 2 . . . 67 55
2 3 . . . . .
3 1 55 1 3 64 50
3 2 . . . 60 47
3 3 . . . 58 46
4 1 38 0 1 50 44
4 2 . . . . .
4 3 . . . 54 46
;
run;


*interview (int) every year, id-1 was recorded age, sex, race, gh and mh data but not other id did so.
1) I want to calculate age, sex and race for id-2, 3 and 4
2) id-2, and id-3 did not have gh and mh data in int3 and int2, respectively, how I handle this situation for age, sex and race

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

One method would be to keep the values in retained variables, another is this:

 

data demo;
input id int age sex race gh mh;
cards;
1 1 45 1 2 56 48
1 2 46 1 2 57 48
1 3 47 1 2 67 60
2 1 32 0 1 67 55
2 2 . . . 67 55
2 3 . . . . .
3 1 55 1 3 64 50
3 2 . . . 60 47
3 3 . . . 58 46
4 1 38 0 1 50 44
4 2 . . . . .
4 3 . . . 54 46
;
run;

data help;
set demo;
by id;
keep id age sex race;
if first.id;
run;

data want;
merge help demo(drop=sex age race);
by id;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

One method would be to keep the values in retained variables, another is this:

 

data demo;
input id int age sex race gh mh;
cards;
1 1 45 1 2 56 48
1 2 46 1 2 57 48
1 3 47 1 2 67 60
2 1 32 0 1 67 55
2 2 . . . 67 55
2 3 . . . . .
3 1 55 1 3 64 50
3 2 . . . 60 47
3 3 . . . 58 46
4 1 38 0 1 50 44
4 2 . . . . .
4 3 . . . 54 46
;
run;

data help;
set demo;
by id;
keep id age sex race;
if first.id;
run;

data want;
merge help demo(drop=sex age race);
by id;
run;
PhanS
Obsidian | Level 7

Dear Kurt,

 

Thanks for your help. 

After running, data merged well.

However, I notice that age for ID-1 changed to 45 in 3 interviews cycle. Because each interview was conducted every year, age of participants should be gained one-year age when interviewing.

 

I am happy to hear more from you or anyone. 

 

Appreciated.

 

Phan S.

 

 

PhanS
Obsidian | Level 7

Dear Kurt,

 

 

Thank you.

 

Phan S.

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!

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.

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