BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bluebonnet16
Fluorite | Level 6

Hi,

I have a dataset with clients who had certain procedure performed over the 3 year time period. I need to flag those instances where  4 or more procedures took place outside the 180 day period for each client ID (status=flagged). Here is a sample data set.

IDserv_dtlagPROCCDproccdcntstatus
2172074013/22/20170959514 
2172074014/2/2018376959514 
2172074014/3/20181959514 
2172074015/23/201850959514 
22013550211/4/20160959514 
22013550211/13/20169959514 
22013550211/14/20161959514 
22013550211/15/20161959514flagged
23728780611/17/20170959516 
23728780611/18/20171959516 
23728780611/19/20171959516 
23728780610/28/2018343959516 
23728780610/29/20181959516 
23728780610/30/20181959516 
22046250110/17/20180959515 
22046250110/18/20181959515 
22046250110/19/20181959515 
22046250110/20/20181959515 
22046250110/21/20181959515flagged
24989200312/7/20160959514 
24989200312/8/20161959514 
24989200312/9/20161959514 
24989200312/10/20161959514flagged
2575275034/14/20170959516 
2575275034/15/20171959516 
2575275035/28/201743959516 
2575275037/18/2018416959516 
2575275037/19/20181959516 
2575275037/20/20181959516 
2194825018/30/201609595111 
2194825018/31/201619595111 
2194825019/1/201619595111 
2194825019/2/201619595111flagged
2194825019/3/201619595111flagged
2194825019/4/201619595111flagged
2194825019/5/201619595111flagged
2194825019/6/201619595111flagged
2194825019/7/201619595111flagged
2194825019/8/201619595111flagged
2194825019/9/201619595111flagged

 

So, for example, the first ID=217207401 will not be flagged. But the second, id=220135502 will be flagged as there were 4 procedures done in less than 180 days.

I understand that there should probably be some way of using a macro to count within each ID group the number of days for the consequitive 4 observations, and flag them when it's more than 180. But I'm not sure how to do that as teh number of rows for each ID is different. Any help or advice would be appreciated.

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Macros and macro variables are most definitely not needed, and not worth the complication.

 

Just sort by ID and serv_dt and then (assuming serv_dt is really a SAS date and not a character string).

 

data want;
    set have;
    prev_id=lag3(id);
    prev_dt=lag3(serv_dt);
    if prev_id=id and serv_dt-prev_dt<180 then status='flagged';
run;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Macros and macro variables are most definitely not needed, and not worth the complication.

 

Just sort by ID and serv_dt and then (assuming serv_dt is really a SAS date and not a character string).

 

data want;
    set have;
    prev_id=lag3(id);
    prev_dt=lag3(serv_dt);
    if prev_id=id and serv_dt-prev_dt<180 then status='flagged';
run;
--
Paige Miller
Bluebonnet16
Fluorite | Level 6

Thank you so much!! It worked great and I understand the logic behind it. Thank you!!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 729 views
  • 1 like
  • 2 in conversation