DATA Step, Macro, Functions and more

Handling non-time varying variables in a longitudinal data

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 83
Accepted Solution

Handling non-time varying variables in a longitudinal data

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


Accepted Solutions
Solution
a month ago
Super User
Posts: 9,878

Re: Handling non-time varying variables in a longitudinal data

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
a month ago
Super User
Posts: 9,878

Re: Handling non-time varying variables in a longitudinal data

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 83

Re: Handling non-time varying variables in a longitudinal data

Posted in reply to KurtBremser

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.

 

 

Super User
Posts: 9,878

Re: Handling non-time varying variables in a longitudinal data

Then you should handle age separately, by incrementing from the first record.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 83

Re: Handling non-time varying variables in a longitudinal data

Posted in reply to KurtBremser

Dear Kurt,

 

 

Thank you.

 

Phan S.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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