Hi all. I'm looking to create a variable that will flag weight-changes between weeks that are greater/ less than 8 pounds. I think a do loop would be appropriate but I'm not exactly sure how because there are multiple entries per ID number...Here's the layout of my data:
ID Date Weight
1 6/6 130
1 6/13 135
1 6/21 128
2 6/6 149
2 6/13 151
2 6/21 146
...
Thanks for your help!
Assuming that each record represents a week and that data is sorted:
data have;
input ID Date $ Weight;
datalines;
1 6/6 130
1 6/13 135
1 6/21 118
2 6/6 149
2 6/13 151
2 6/21 146
;
data want;
do until(last.id);
set have; by id;
if not first.id then flag = abs(Weight-lastWeekWeight) > 8;
output;
lastWeekWeight = weight;
end;
drop lastWeekWeight;
run;
proc print; run;
Obs ID Date Weight flag 1 1 6/6 130 . 2 1 6/13 135 0 3 1 6/21 118 1 4 2 6/6 149 . 5 2 6/13 151 0 6 2 6/21 146 0
Assuming that each record represents a week and that data is sorted:
data have;
input ID Date $ Weight;
datalines;
1 6/6 130
1 6/13 135
1 6/21 118
2 6/6 149
2 6/13 151
2 6/21 146
;
data want;
do until(last.id);
set have; by id;
if not first.id then flag = abs(Weight-lastWeekWeight) > 8;
output;
lastWeekWeight = weight;
end;
drop lastWeekWeight;
run;
proc print; run;
Obs ID Date Weight flag 1 1 6/6 130 . 2 1 6/13 135 0 3 1 6/21 118 1 4 2 6/6 149 . 5 2 6/13 151 0 6 2 6/21 146 0
This code worked well; thank 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.