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

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. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You need:

  • BY,  This assumes your data is sorted by ID
  • RETAIN to hold the value across rows
  • FIRST to identify the first record. In your case you could also use VISIT=1

 

data want;
set have;

by id;

RETAIN education_baseline;

if first.id then education_baseline=education;

run;

View solution in original post

2 REPLIES 2
Reeza
Super User

You need:

  • BY,  This assumes your data is sorted by ID
  • RETAIN to hold the value across rows
  • FIRST to identify the first record. In your case you could also use VISIT=1

 

data want;
set have;

by id;

RETAIN education_baseline;

if first.id then education_baseline=education;

run;
Satish_Parida
Lapis Lazuli | Level 10
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. 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 1109 views
  • 2 likes
  • 3 in conversation