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

Hello SAS Gurus,

 

I am programming a dataset where I want to know the current use of 2 or more medications and the number of days of concurrent use. There are some old threads related to the similar topic here but I couldn't find the solution I am looking for. So I request SAS gurus to please help me.

 

The dataset I have;

 

ID              DRUG                 PRES_ST_DT            PRES_END_DT          DAYSSUPP

1                 A                          11NOV2021               10DEC2021                 30

1                 A                          19NOV2021               18DEC2021                 30

1                A                          10DEC2021                08JAN2022                30

1                B                           12NOV2021                26NOV2021               15

1                B                            05JAN2022              19JAN2022                15

2               A                            10OCT2021               08NOV2021               30

2               A                            12NOV2021               11DEC2021                  30

2               A                             08DEC2021              06JAN2022                30

2               C                             08NOV2021             12NOV2021                 5

2                B                             12DEC2021               26DEC2021                15

2                B                             02JAN2022               16JAN2022               15

3               A                              03AUG2021             01SEP2021                  30

3               A                              05SEP2021               04OCT2021               30

3               A                              10NOV2021              09DEC2021                30

3               B                               06OCT2021             20OCT2021                15

4               A                              14AUG2021              12SEP2021                  30

4               A                               15SEP2021               14OCT2021                30

4               A                                14OCT2021             12NOV2021                30

4               A                                10NOV2021             09DEC2021               30

4               B                                 20MAR2021            03APR2021               15

4               C                                 10APR2021              09MAY2021               30

5               A                                 21MAY2021              19JUN2021                 30

5               A                                  15JUN2021              14JUL2021                  30

5               A                                  13JUL2021               11AUG2021                 30

5              A                                    15AUG2021             13SEP2021                 30

5              A                                   10SEP2021               09OCT2021              30

5              D                                    25MAY2021            23JUN2021                30

5              D                                    15JUN2021               14JUL2021                30

5              D                                    15AUG2021               13SEP2021               30

5              C                                     10AUG2021              08SEP2021              30

6              A                                     17MAR2021              15APR2021             30

6              A                                     07APR2021              06APR2021            30

6              E                                      17MAR2021             15APR2021              30

6              E                                      07APR2021              06APR2021            30

 

The results I am looking for;

Pt 1 has concurrent use of drug A and B for 19 days

Pt 2 has concurrent use of drug A and C for 2 days, and A and B for 20 days

Pt 3 and 4 has no concurrent use of meds

Pt 5 has concurrent use of drugs A and D for 61 days and A, C and D for 25 days

Pt 6 has concurrent use of drugs A and E for 52 days (not 60 days)

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input ID              DRUG        $         PRES_ST_DT     :date9.       PRES_END_DT  :date9.  ;
format PRES_ST_DT         PRES_END_DT date9.   ;
cards;
1                 A                          11NOV2021               10DEC2021                 30
1                 A                          19NOV2021               18DEC2021                 30
1                A                          10DEC2021                08JAN2022                30
1                B                           12NOV2021                26NOV2021               15
1                B                            05JAN2022              19JAN2022                15
2               A                            10OCT2021               08NOV2021               30
2               A                            12NOV2021               11DEC2021                  30
2               A                             08DEC2021              06JAN2022                30
2               C                             08NOV2021             12NOV2021                 5
2                B                             12DEC2021               26DEC2021                15
2                B                             02JAN2022               16JAN2022               15
3               A                              03AUG2021             01SEP2021                  30
3               A                              05SEP2021               04OCT2021               30
3               A                              10NOV2021              09DEC2021                30
3               B                               06OCT2021             20OCT2021                15
4               A                              14AUG2021              12SEP2021                  30
4               A                               15SEP2021               14OCT2021                30
4               A                                14OCT2021             12NOV2021                30
4               A                                10NOV2021             09DEC2021               30
4               B                                 20MAR2021            03APR2021               15
4               C                                 10APR2021              09MAY2021               30
5               A                                 21MAY2021              19JUN2021                 30
5               A                                  15JUN2021              14JUL2021                  30
5               A                                  13JUL2021               11AUG2021                 30
5              A                                    15AUG2021             13SEP2021                 30
5              A                                   10SEP2021               09OCT2021              30
5              D                                    25MAY2021            23JUN2021                30
5              D                                    15JUN2021               14JUL2021                30
5              D                                    15AUG2021               13SEP2021               30
5              C                                     10AUG2021              08SEP2021              30
6              A                                     17MAR2021              15APR2021             30
6              A                                     07APR2021              06APR2021            30
6              E                                      17MAR2021             15APR2021              30
6              E                                      07APR2021              06MAY2021            30
;
data temp;
 set have;
 do date=PRES_ST_DT to PRES_END_DT ;
   output;
 end;
 format date date9.;
 keep ID              DRUG    date;
run;
proc sort data=temp nodupkey;
by id date DRUG;
run;
data temp2;
do until(last.date);
 set temp;
 by id date;
 length drugs $ 80;
 drugs=cats(drugs,drug);
end;
if length(drugs)>1;
drop drug;
run;
proc freq data=temp2 noprint;
table id*drugs/out=want list;
run;

View solution in original post

8 REPLIES 8
abhi309
Obsidian | Level 7

Hi Kurt,

Thank you for your reply. I want to add the extra supply just like you mentioned for these calculations. Example ID 1 has continuous supply of drug A from 11NOV2021 to 09FEB2022. So if he is taking a second drug at any point of time from 11NOV2021 to 09FEB2022 that will be considered as concurrent use. This might change some of the results which I mentioned in my first post.

 

Thanks again

Kurt_Bremser
Super User
data have;
input
  id $
  drug $
  pres_st_dt :date9.
  pres_end_dt :date9.
  dayssupp
;
format
  pres_st_dt pres_end_dt yymmdd10.
;
datalines;
1 A 11NOV2021 10DEC2021 30
1 A 19NOV2021 18DEC2021 30
1 A 10DEC2021 08JAN2022 30
1 B 12NOV2021 26NOV2021 15
1 B 05JAN2022 19JAN2022 15
2 A 10OCT2021 08NOV2021 30
2 A 12NOV2021 11DEC2021 30
2 A 08DEC2021 06JAN2022 30
2 C 08NOV2021 12NOV2021  5
2 B 12DEC2021 26DEC2021 15
2 B 02JAN2022 16JAN2022 15
3 A 03AUG2021 01SEP2021 30
3 A 05SEP2021 04OCT2021 30
3 A 10NOV2021 09DEC2021 30
3 B 06OCT2021 20OCT2021 15
4 A 14AUG2021 12SEP2021 30
4 A 15SEP2021 14OCT2021 30
4 A 14OCT2021 12NOV2021 30
4 A 10NOV2021 09DEC2021 30
4 B 20MAR2021 03APR2021 15
4 C 10APR2021 09MAY2021 30
5 A 21MAY2021 19JUN2021 30
5 A 15JUN2021 14JUL2021 30
5 A 13JUL2021 11AUG2021 30
5 A 15AUG2021 13SEP2021 30
5 A 10SEP2021 09OCT2021 30
5 D 25MAY2021 23JUN2021 30
5 D 15JUN2021 14JUL2021 30
5 D 15AUG2021 13SEP2021 30
5 C 10AUG2021 08SEP2021 30
6 A 17MAR2021 15APR2021 30
6 A 07APR2021 06APR2021 30
6 E 17MAR2021 15APR2021 30
6 E 07APR2021 06APR2021 30
;

/* sort, so that the next step works */
proc sort data=have;
by id drug;
run;

/* set dimensionsfor the arrays in the next step */
%let start = %sysfunc(inputn(2000-01-01,yymmdd10.));
%let end = %sysfunc(inputn(2024-12-31,yymmdd10.));

/* record the use of a particular drug by a patient; overlapping prescriptions are expanded */
/* then, the drugs are cumulated for each patient, and output to a dataset with dates */
data want;
set have;
by id drug;
array drugs{&start.:&end.} _temporary_;
array this_drug{&start.:&end.} _temporary_;
format day yymmdd10.;
if first.id
then do day = &start. to &end.;
  drugs{day} = 0;
end;
if first.drug
then do day = &start. to &end.;
  this_drug{day} = 0;
end;
day = pres_st_dt;
do while (day le pres_end_dt);
  if this_drug{day} = 0
  then this_drug{day} = 1;
  else pres_end_dt + 1;
  day + 1;
end;
if last.drug
then do day = &start. to &end.;
  drugs{day} + this_drug{day};
end;
if last.id
then do day = &start. to &end.;
  if drugs{day} > 1
  then do;
    no_of_drugs = drugs{day};
    output;
  end;
end;
keep id day no_of_drugs;
run;

/* the result can be used in PROC FREQ, for example */
proc freq data=want;
tables id*no_of_drugs;
run;
abhi309
Obsidian | Level 7

Thank you @Kurt_Bremser. This is what I am exactly looking for. 

 

Ksharp
Super User
data have;
input ID              DRUG        $         PRES_ST_DT     :date9.       PRES_END_DT  :date9.  ;
format PRES_ST_DT         PRES_END_DT date9.   ;
cards;
1                 A                          11NOV2021               10DEC2021                 30
1                 A                          19NOV2021               18DEC2021                 30
1                A                          10DEC2021                08JAN2022                30
1                B                           12NOV2021                26NOV2021               15
1                B                            05JAN2022              19JAN2022                15
2               A                            10OCT2021               08NOV2021               30
2               A                            12NOV2021               11DEC2021                  30
2               A                             08DEC2021              06JAN2022                30
2               C                             08NOV2021             12NOV2021                 5
2                B                             12DEC2021               26DEC2021                15
2                B                             02JAN2022               16JAN2022               15
3               A                              03AUG2021             01SEP2021                  30
3               A                              05SEP2021               04OCT2021               30
3               A                              10NOV2021              09DEC2021                30
3               B                               06OCT2021             20OCT2021                15
4               A                              14AUG2021              12SEP2021                  30
4               A                               15SEP2021               14OCT2021                30
4               A                                14OCT2021             12NOV2021                30
4               A                                10NOV2021             09DEC2021               30
4               B                                 20MAR2021            03APR2021               15
4               C                                 10APR2021              09MAY2021               30
5               A                                 21MAY2021              19JUN2021                 30
5               A                                  15JUN2021              14JUL2021                  30
5               A                                  13JUL2021               11AUG2021                 30
5              A                                    15AUG2021             13SEP2021                 30
5              A                                   10SEP2021               09OCT2021              30
5              D                                    25MAY2021            23JUN2021                30
5              D                                    15JUN2021               14JUL2021                30
5              D                                    15AUG2021               13SEP2021               30
5              C                                     10AUG2021              08SEP2021              30
6              A                                     17MAR2021              15APR2021             30
6              A                                     07APR2021              06APR2021            30
6              E                                      17MAR2021             15APR2021              30
6              E                                      07APR2021              06MAY2021            30
;
data temp;
 set have;
 do date=PRES_ST_DT to PRES_END_DT ;
   output;
 end;
 format date date9.;
 keep ID              DRUG    date;
run;
proc sort data=temp nodupkey;
by id date DRUG;
run;
data temp2;
do until(last.date);
 set temp;
 by id date;
 length drugs $ 80;
 drugs=cats(drugs,drug);
end;
if length(drugs)>1;
drop drug;
run;
proc freq data=temp2 noprint;
table id*drugs/out=want list;
run;
abhi309
Obsidian | Level 7

Thank you @Ksharp for the solution. The only issue is that this program does not take of extra meds patients have in hand if they fill their next prescription before finishing the previous one.

 

 

Ksharp
Super User
I don't understand your question.
Can you post your data,and the output ,and explain your logic?
whymath
Lapis Lazuli | Level 10
Some dates are wrong, like the last row, the end date is earlier than start date.

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
  • 8 replies
  • 749 views
  • 2 likes
  • 4 in conversation