SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1250 views
  • 0 likes
  • 2 in conversation