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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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