BookmarkSubscribeRSS Feed
Paakay
Calcite | Level 5

I have ID's with multiple flags in cycles however some id's flags are not consecutive. For example ID A has two flags in cycle 1 but none in cycle 2,3,4 but then has another flag in cycle 5. I want to keep only the flags in cycle 1 and delete flags in cycle 5 since there is a break in cycle at 2. Another example is ID B has flags in cycles 1, 2,3 but not in cycle 4 however has flags in cycles 5 and 6. Since there is a break in cycle at 4, I want to keep the consecutive flags in cycles 1,2,3 and delete cycles 5 and 6. Can someone help me with this. Sample data is below

DATA chk;
INPUT ID $ Flag Cycle ;
CARDS;
A 1 1
A 1 1
A 1 5
A 1 5
B 1 1
B 1 2
B 1 3
B 1 5
B 1 6
C 1 1
C 1 1
C 1 2
C 1 4
C 1 5
;
RUN;

  

This is the output I want

 
 
         ID.     FLG  CYC
1A11 
2A11 
3B11 
4B12 
5B13 
6C11 
7C11 
8C12
1 REPLY 1
ChrisNZ
Tourmaline | Level 20

Like this?

data HAVE;
  input ID $ FLAG CYCLE ;
cards;
A 1 1
A 1 1
A 1 5
A 1 5
B 1 1
B 1 2
B 1 3
B 1 5
B 1 6
C 1 1
C 1 1
C 1 2
C 1 4
C 1 5
run;

data WANT;  
  set HAVE;
  by ID;
  DEL_FLG= ifn( first.ID           , .
         , ifn( CYCLE>lag(CYCLE)+1 , 1
         , DEL_FLG ));
  if DEL_FLG then delete;
  retain DEL_FLG;
run;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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