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. 

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 675 views
  • 2 likes
  • 3 in conversation