SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
srbeer
Calcite | Level 5

Hello!

 

I have a dataset, with an ID and an event date. 

 

data have;
input ID event_date : DATE9. ;
format event_date DATE9.;
cards;
18901 01JUL2022
18901 14JUL2022
18901 15JUL2022
18901 30JUL2022
18901 31JUL2022
18901 15AUG2022
;

Once the first event is encountered, I want to flag that row first. Then flag any row that occurs outside a 14 day time window. So, skip row 2 since it's within 14 days and flag row 3. Once Row 3 is flagged, the 14 day counter starts again and row 4 gets flagged. Row 5 is skipped now with the new 14 day counter in effect and then flag row 6 that occurs outside the 14 day counter and so on for any number of iterations.

Tried several ways, but I'm mainly struggling with resetting the 14 day counter once a flag's activated. Any help's much appreciated.

want:

IDEvent_DateFlag
1890101JUL20221
1890114JUL20220
1890115JUL20221
1890130JUL20221
1890131JUL20220
1890115AUG20221

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input ID event_date : DATE9. ;
format event_date DATE9.;
cards;
18901 01JUL2022
18901 14JUL2022
18901 15JUL2022
18901 30JUL2022
18901 31JUL2022
18901 15AUG2022
18902 01JUL2022
18902 14JUL2022
18902 15JUL2022
18902 30JUL2022
18902 31JUL2022
18902 15AUG2022
;

data want(drop = c);
   set have;
   by ID;

   c + dif(event_date);

   if first.ID | c ge 14 then do;
      flag = 1;
      c = 0;
   end;
   
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20
data have;
input ID event_date : DATE9. ;
format event_date DATE9.;
cards;
18901 01JUL2022
18901 14JUL2022
18901 15JUL2022
18901 30JUL2022
18901 31JUL2022
18901 15AUG2022
18902 01JUL2022
18902 14JUL2022
18902 15JUL2022
18902 30JUL2022
18902 31JUL2022
18902 15AUG2022
;

data want(drop = c);
   set have;
   by ID;

   c + dif(event_date);

   if first.ID | c ge 14 then do;
      flag = 1;
      c = 0;
   end;
   
run;
srbeer
Calcite | Level 5
thanks for the awesome solution Peter! I had like 100 lines of code typed in with transposes and unsuccessful lag functions
PeterClemmensen
Tourmaline | Level 20

Anytime 🙂 We all write ourselves into dead ends now and then..

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 3 replies
  • 825 views
  • 0 likes
  • 2 in conversation