DATA Step, Macro, Functions and more

iteration of the first value in a group of observations

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

iteration of the first value in a group of observations

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. 

 


Accepted Solutions
Solution
‎02-13-2018 11:32 AM
Super User
Posts: 24,010

Re: iteration of the first value in a group of observations

Posted in reply to Sinakian1

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


All Replies
Solution
‎02-13-2018 11:32 AM
Super User
Posts: 24,010

Re: iteration of the first value in a group of observations

Posted in reply to Sinakian1

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;
Frequent Contributor
Posts: 112

Re: iteration of the first value in a group of observations

Posted in reply to Sinakian1
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. 

☑ This topic is solved.

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

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