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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 670 views
  • 0 likes
  • 2 in conversation