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!
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: