Hi @saipriya92 ,
You can try this. Hope this help!
/* Expand dataset: create one row per day for each drug */
data sample1_step1_exp;
set sample1_step1;
format DAY date9.;
do DAY=STDT to ENDDT;
output;
end;
run;
proc sort data=sample1_step1_exp;
by PTID DAY;
run;
/* Transpose drugs to create combination regimens */
proc transpose data=sample1_step1_exp out=sample1_step1_tr (drop=_name_);
var DRUG;
by PTID DAY;
run;
data sample1_step1_regimen1;
set sample1_step1_tr;
REGIMEN = catx(" + ",of col:);
keep PTID DAY REGIMEN;
run;
/* Creation of the variable COUNTER, which is incremented each time a new regimen is given */
data sample1_step1_regimen2;
set sample1_step1_regimen1;
by PTID REGIMEN notsorted;
if first.PTID then COUNTER=0;
if first.REGIMEN then COUNTER + 1;
run;
/* Creation of the final table without "blank" periods of time */
proc sql;
create table sample1_step1_regimen3 as
select PTID,
min(DAY) as STDT format=date9.,
max(DAY) as ENDDT format=date9.,
REGIMEN
from sample1_step1_regimen2 group by PTID, REGIMEN, COUNTER order by PTID, STDT ;
quit;
/* Identification of "blank" periods of time */
data sample1_step1_blank (keep= PTID REGIMEN _STDT_blank _ENDDT_blank );
set sample1_step1_regimen3;
format _STDT_blank _ENDDT_blank date9.;
by PTID;
_STDT_blank = lag(ENDDT) +1 ;
if first.PTID or _STDT_blank = STDT then _STDT_blank=.;
if _STDT_blank ne . then do;
_ENDDT_blank = STDT - 1;
REGIMEN = "Blank";
output;
end;
run;
/* Creation of the final table (retrieve "blank" periods of time) */
data sample1_step2;
set sample1_step1_regimen3 sample1_step1_blank (rename=(_STDT_blank= STDT _ENDDT_blank=ENDDT));
run;
proc sort data=sample1_step2;
by PTID STDT;
run;
Output:
... View more