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

Hi all,

 

I am having a really hard time attempting to flag users that are concurrently using two different classes of medications. My goal is to flag any dispensing of a drug if it overlaps with a drug of another class on the start of the dispensing.

 

I have created an example data set to show what I mean. So essentially I have:

 

ObsUserDrug_ClassYear_MonthStart_dateEnd_Date
11A20110101/01/201101/20/2011
21A20110101/15/201102/15/2011
31B20110202/01/201102/10/2011
41A20110202/11/201102/20/2011
52A20110101/05/201101/20/2011
62B20110101/15/201102/15/2011
72A20110101/12/201101/30/2011


Where class A and Class B are two different drug classes. I only care about when there is an overlap of use of a drug in Class A and Class B, not when there is an overlap of drug in the same class. So ultimately, I would want a new flag variable that would look like this:

 

 

ObsUserDrug_ClassYear_MonthStart_dateEnd_DateFlag
11A20110101/01/201101/20/20110
21A20110101/15/201102/15/20110
31B20110202/01/201102/10/20111
41A20110202/11/201102/20/20110
52A20110101/05/201101/20/20110
62B20110101/15/201102/15/20111
72A20110101/12/201101/30/20111

 

So observation 2 doesn't get a flag because it's an overlap of a drug in the same class, but then on the dispensing in observation 3 would get a flag since it is a dispensing of a drug in class B while they were taking a drug of class A.

 

The "Year_Month" variable is only relevant because I want to know the month in which they started concurrent use of a drug in class A and B.

 

Any help would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

A reasonable (but untested, that's up to you) approach:

 

proc sort data=have;
   by user start_date;
run;

data want;
set have;
by user drug_class notsorted;
prior_end = lag(end_date);
if first.user=0 and first.drug_class=1 and start_date <= prior_end then flag=1;
else flag=0;
run;

In this case, I'm assuming that "same day" is an overlap.  If that's not the definition, then change "<=" to "<"

View solution in original post

2 REPLIES 2
Astounding
PROC Star

A reasonable (but untested, that's up to you) approach:

 

proc sort data=have;
   by user start_date;
run;

data want;
set have;
by user drug_class notsorted;
prior_end = lag(end_date);
if first.user=0 and first.drug_class=1 and start_date <= prior_end then flag=1;
else flag=0;
run;

In this case, I'm assuming that "same day" is an overlap.  If that's not the definition, then change "<=" to "<"

PGStats
Opal | Level 21

This will flag all overlaps:

 

data have;
input User Drug_Class $ Year_Month $ (Start_date End_Date) (:mmddyy10.);
format start_date end_date yymmdd10.;
datalines;
1 A 201101 01/01/2011 01/20/2011
1 A 201101 01/15/2011 02/15/2011
1 B 201102 02/01/2011 02/10/2011
1 A 201102 02/11/2011 02/20/2011
2 A 201101 01/05/2011 01/20/2011
2 B 201101 01/15/2011 02/15/2011
2 A 201101 01/12/2011 01/30/2011
;

proc sql;
select 
	a.user,
	a.drug_class as class1,
	b.drug_class as class2,
	max(a.start_date, b.start_date) as overlap_start format=yymmdd10.,
	min(a.end_date, b.end_date) as overlap_end format=yymmdd10.,
	calculated overlap_end - calculated overlap_start + 1 as overlap_days
from 
	have as a inner join
	have as b on a.user=b.user and a.drug_class < b.drug_class and
		max(a.start_date, b.start_date) <= min(a.end_date, b.end_date);
quit;
User 	class1 	class2 	overlap_start 	overlap_end 	overlap_days
1 	A 	B 	2011-02-01 	2011-02-10 	10
2 	A 	B 	2011-01-15 	2011-01-20 	6
2 	A 	B 	2011-01-15 	2011-01-30 	16
PG

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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