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

I have the following table that cumulate value along the day by id and when the treshold (-150) is exceeded i need a flag and keep it until last.id

 

IDvaluecumulative_value

flag

1

35

350
135700
11001700
1-110600
135950
215150
235500
2-130-800
340400
3-125-850
3-90-1751
335-1401
335-1051
335-701

 

I've tried a lot of lag and retains structures but it doesn't work.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

A straightforward approach could work as follows:

 

data want;
   set have;
   by id;
   retain flag;
   if first.id then flag=0;
   if cumulative_value < -150 then flag=1;
run;

 This assumes you know how to get the cumulative value (or perhaps it is already in your data.)  If you need to compute it along the way, here's a modification that would do that:

data want;
   set have;
   retain flag;
   if first.id then do;
      flag=0;
      cumulative_value=0;
   end;
   cumulative_value + value;
   if cumulative_value < -150 then flag=1;
run;

 

View solution in original post

2 REPLIES 2
Astounding
PROC Star

A straightforward approach could work as follows:

 

data want;
   set have;
   by id;
   retain flag;
   if first.id then flag=0;
   if cumulative_value < -150 then flag=1;
run;

 This assumes you know how to get the cumulative value (or perhaps it is already in your data.)  If you need to compute it along the way, here's a modification that would do that:

data want;
   set have;
   retain flag;
   if first.id then do;
      flag=0;
      cumulative_value=0;
   end;
   cumulative_value + value;
   if cumulative_value < -150 then flag=1;
run;

 

goularts
Calcite | Level 5

Worked perfectly!! Just made a change and included "by id":

data want;
   set have;
   by id;
   retain flag;
   if first.id then do;
      flag=0;
      cumulative_value=0;
   end;
   cumulative_value + value;
   if cumulative_value < -150 then flag=1;
run;

thanks

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 351 views
  • 1 like
  • 2 in conversation