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:
Obs | User | Drug_Class | Year_Month | Start_date | End_Date |
1 | 1 | A | 201101 | 01/01/2011 | 01/20/2011 |
2 | 1 | A | 201101 | 01/15/2011 | 02/15/2011 |
3 | 1 | B | 201102 | 02/01/2011 | 02/10/2011 |
4 | 1 | A | 201102 | 02/11/2011 | 02/20/2011 |
5 | 2 | A | 201101 | 01/05/2011 | 01/20/2011 |
6 | 2 | B | 201101 | 01/15/2011 | 02/15/2011 |
7 | 2 | A | 201101 | 01/12/2011 | 01/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:
Obs | User | Drug_Class | Year_Month | Start_date | End_Date | Flag |
1 | 1 | A | 201101 | 01/01/2011 | 01/20/2011 | 0 |
2 | 1 | A | 201101 | 01/15/2011 | 02/15/2011 | 0 |
3 | 1 | B | 201102 | 02/01/2011 | 02/10/2011 | 1 |
4 | 1 | A | 201102 | 02/11/2011 | 02/20/2011 | 0 |
5 | 2 | A | 201101 | 01/05/2011 | 01/20/2011 | 0 |
6 | 2 | B | 201101 | 01/15/2011 | 02/15/2011 | 1 |
7 | 2 | A | 201101 | 01/12/2011 | 01/30/2011 | 1 |
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!
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 "<"
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 "<"
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.