BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jmmedina25
Obsidian | Level 7

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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
PG

View solution in original post

3 REPLIES 3
Astounding
PROC Star
The DATA step functions like a DO loop automatically, reading one observation then the next, then the next. Try it this way:

data want;
set have;
by id;
change = dif(weight);
if first.id=0 then do;
if change > 8 then flag ="+";
else if change < -8 then flag ="-";
end;
run;
PGStats
Opal | Level 21

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
PG
jmmedina25
Obsidian | Level 7

This code worked well; thank 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
  • 3 replies
  • 2097 views
  • 2 likes
  • 3 in conversation