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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 498 views
  • 0 likes
  • 2 in conversation