BookmarkSubscribeRSS Feed
Paakay
Calcite | Level 5

Hello All,  I want to flag ID's with consecutive flags as 1 and those without consecutive flags as 0. For example ID 1 is consecutive because it starts from cycle 1 to cycle 4 so I create a flag for ID 1 as 1. However for ID's with no consecutive cycle numbers like ID 2 (its starts from cycle 2 to cycle 5, missing cycle 1) and ID 3 also starts from cycle 8 missing cycles 1-7,  I flags these ID's as 0. Is there a way to go about this. Thank you.

 

Data have;
 input ID $1. Cycle;
Datalines;
1    1
1    1
1    2
1    3
1    4
2   3
2   4
2   5
3   8
3   8
;
run; 

 

1 REPLY 1
Kurt_Bremser
Super User

Run a DO loop:

data want;
flag = 1;
cy = -1;
do until (last.id);
  set have;
  by id;
  if cycle gt cy + 1 then flag = 0;
  cy = cycle;
end;
keep id flag;
run;

(untested, posted from my tablet)

 

If you want to "remerge" the flag to the original dataset, add a second DO loop that reads the same group and OUTPUTs.

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
  • 1 reply
  • 422 views
  • 0 likes
  • 2 in conversation