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.
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.
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.