Hi and thank you so much for helping me with my previous question. I would really appreciate if you could please help me with this one too.
I have a data set with multiple observations for each ID. I want to keep the baseline value for education variable. How can I iterate the first value for the variable "education" in all the observations for each caseid (by caseid). Hereunder, I have included what_I_Have and What_I_Want datalines.
data what_I_Have;
input id visit education;
datalines;
1000 1 1
1000 2 1
1000 3 2
1000 4 1
1001 1 .
1001 2 1
1001 3 2
1002 1 2
1002 2 1
1003 1 1
1003 2 2
1003 3 2
1003 4 2
1004 1 .
1004 2 1
1004 3 1
;
run;
data what_I_Have;
input id visit education;
datalines;
1000 1 1
1000 2 1
1000 3 1
1000 4 1
1001 1 .
1001 2 .
1001 3 .
1002 1 2
1002 2 2
1003 1 1
1003 2 1
1003 3 1
1003 4 1
1004 1 .
1004 2 .
1004 3 .
;
run;
Thank you so much for your help.
You need:
data want;
set have;
by id;
RETAIN education_baseline;
if first.id then education_baseline=education;
run;
You need:
data want;
set have;
by id;
RETAIN education_baseline;
if first.id then education_baseline=education;
run;
data what_I_Have;
input id visit education;
datalines;
1000 1 1
1000 2 1
1000 3 2
1000 4 1
1001 1 .
1001 2 1
1001 3 2
1002 1 2
1002 2 1
1003 1 1
1003 2 2
1003 3 2
1003 4 2
1004 1 .
1004 2 1
1004 3 1
;
run;
proc sort data=what_I_Have;
by id visit;
run;
data what_I_Want(rename=(education1=education));
set what_I_Have;
by id visit;
retain education1;
if first.id then education1=education;
drop education;
run;
Please Let us know if it worked for you.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.