BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
jmmedina25
Obsidian | Level 7

Hello,  I'm looking to create a flag for members who have 2 or more different codes within the span of 30 days and the same location.  There are 4 possible codes a member (24, 25, 26, 27) can have and they can have the same code more than once.  Here's and example of the data and the flag I would like to create:

 

Memberid    date              locationid   code      

1                  03Feb2022    12               24        

1                  24Feb2022    12               24

1                  28Mar2022    13               24

2                  03Feb2022    13               24        

2                  24Feb2022    13               25

3                  03Feb2022    12               24        

3                  24Feb2022    13               26

 

output data:

Memberid   Flag      

1                 0       

2                 1

3                 0         

 

        

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

If the data are sorted by memberid/locationid/date, then this should do:

 

data want (keep=memberid flag);
  set have;
  by memberid locationid date ;
  if first.memberid=1 then flag=0;
  retain flag;
  if first.locationid=0 and code^=lag(code) and dif(date)<=30 then flag=1;
  if last.memberid;
run;
  

Flag is set to zero at first record for a memberid.  All subsequent records are checked to see if flag should be 1.  It's never reset from 1 to zero for a memberid.

 

The flag check tests whether (1) the record in hand is NOT the start of a locationid, (2) the current code differs from the prior code, and (3) the current date minus last date <=30.  

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
proc sql;
    create table want as select memberid,count(distinct code)>1 as flag
    from have 
    group by memberid;
quit;

This should match your output. However, your output does not match your description of the problem, the 30 day period is ignored. Perhaps you can explain how that is used, and what the output should look like if the 30 day time period is used.

--
Paige Miller
mkeintz
PROC Star

If the data are sorted by memberid/locationid/date, then this should do:

 

data want (keep=memberid flag);
  set have;
  by memberid locationid date ;
  if first.memberid=1 then flag=0;
  retain flag;
  if first.locationid=0 and code^=lag(code) and dif(date)<=30 then flag=1;
  if last.memberid;
run;
  

Flag is set to zero at first record for a memberid.  All subsequent records are checked to see if flag should be 1.  It's never reset from 1 to zero for a memberid.

 

The flag check tests whether (1) the record in hand is NOT the start of a locationid, (2) the current code differs from the prior code, and (3) the current date minus last date <=30.  

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
data have;
input Memberid    date    : date9.          locationid   code      ;
format date date9.;
cards;
1                  03Feb2022    12               24        
1                  24Feb2022    12               24
1                  28Mar2022    13               24
2                  03Feb2022    13               24        
2                  24Feb2022    13               25
3                  03Feb2022    12               24        
3                  24Feb2022    13               26
;

proc sql;
create table want as
 select memberid,max(flag) as flag
  from (
select memberid,locationid,count(distinct code)>1 and range(date)<=30 as flag
    from have 
    group by memberid,locationid
)
group by memberid
;
quit;
mkeintz
PROC Star

@Ksharp wrote:
data have;
input Memberid    date    : date9.          locationid   code      ;
format date date9.;
cards;
1                  03Feb2022    12               24        
1                  24Feb2022    12               24
1                  28Mar2022    13               24
2                  03Feb2022    13               24        
2                  24Feb2022    13               25
3                  03Feb2022    12               24        
3                  24Feb2022    13               26
;

proc sql;
create table want as
 select memberid,max(flag) as flag
  from (
select memberid,locationid,count(distinct code)>1 and range(date)<=30 as flag
    from have 
    group by memberid,locationid
)
group by memberid
;
quit;

This is different than how I read the OP's request

      "2 or more different codes within the span of 30 days and the same location"

I took it to mean that a given location may have a total date range >30 but would still get FLAG=1 if any two distinct codes were within 30 days of each other.

 

@jmmedina25:   If instead you mean that total range of all dates is <=30 then use @Ksharp's suggestion and discard my reply.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
Yeah. Maybe I understand wrong . It is all depend what OP want. OP can test it and pick up the correct one .

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
  • 5 replies
  • 425 views
  • 0 likes
  • 4 in conversation