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!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.