DATA Step, Macro, Functions and more

Treatment episode output

Frequent Contributor
Posts: 121

Treatment episode output

Thanks in advance.

I have the data that contains patienid, drugname, drugstartdate, drugenddate. Same patient can have multiple drugs and even within same drug there can be multiple drugstartdate and drugenddates. 

data have;

input ID      DRUG $  START_DT :mmddyy.      END_DT :mmddyy.;


1     A       2/17/10 3/19/10

1     A       7/6/10  8/5/10

1     C       7/9/11  9/7/11

1     E       3/1/10  5/30/10

2     B       4/1/12  5/31/12

2     A       7/1/10  7/31/10

2     C       8/3/10  11/1/10

2     D       11/1/13 1/30/14

2     E       12/5/13 3/5/14

2     A       2/1/11  5/2/11

2      F      12/16/13 1/14/14


I want to create treatment episode for each patient, where the concomitant meds  (taking different meds at the same time) are identified. Also the gaps have a row created with drug name as blank.  So, for Id A, my new dataset should be like this:


Id                                druglist            streat                           endtreat

1                                  A                     02/17/10                      03/01/10

1                                  AE                   03/1/10                        03/19/10

1                                  E                      03/19/10                      05/30/10

1                                  blank               06/1/10                        07/5/10

1                            A                     07/06/10                           08/5/10

1                           blank                08/6/10                              07/8/11

1                           C                      07/9/11                              09/7/11



Esteemed Advisor
Posts: 5,621

Re: Treatment episode output

A similar problem was addessed last August


The same approach (an array with one element per day) could be used here.

Frequent Contributor
Posts: 121

Re: Treatment episode output

Thanks Sir, but its quite different problem as here we need the names of drugs concatenated instead. Also, i dont understand that code either.
Frequent Contributor
Posts: 121

Re: Treatment episode output

Ok, so I modified your code and almost got what I wanted. Except that i cannot have extra rows for blank periods. Can anybody help in that. Below is the code I used.

data expand;
set sa.sample1_step1;
do date = stdt to enddt;
format date yymmdd10.;
keep ptid date drug;

proc sql;
create table sumDoses as
select ptid, date, drug
from expand
group by ptid, date
order by ptid, date;
proc sort data = sumdoses;
by ptid date drug;
proc transpose data = sumdoses out = trans;
by ptid date;
var drug;

data trans1;
set trans;
drug = catx('+', col1, col2, col3);
keep ptid date drug ;

data want;
set trans1;
by ptid drug notsorted;
retain date_start;
if first.drug then date_start =  date;
if last.drug then do;
    date_stop = date;
format date_start date_stop yymmdd10.;
keep ptid drug date_start date_stop;
Ask a Question
Discussion stats
  • 3 replies
  • 2 in conversation