data WORK.TESTING;
infile datalines dsd truncover;
input PATID:32. Therapy:$1. serv_date1:DATE9. serv_date2:DATE9. serv_date3:DATE9.
serv_date4:DATE9. serv_date5:DATE9. days_supply1:32. days_supply2:32.
days_supply3:32. days_supply4:32. days_supply5:32.;
datalines4;
1,A,11JAN2016,13APR2016,18JUL2016,28OCT2016,,90,90,90,90,
1,B,18JUN2016,22SEP2016,13DEC2016,28DEC2016,,90,90,30,30,
2,A,11JAN2016,18APR2016,14JUL2016,13OCT2016,,90,90,90,90,
2,B,11JAN2016,16APR2016,18JUL2016,13OCT2016,22NOV2016,90,90,90,90,90
3,A,02MAR2016,29AUG2016,05NOV2016,,,90,90,90,,
3,B,05FEB2016,28JUN2016,06SEP2016,02DEC2016,,90,90,90,90,
3,C,26MAR2016,27JUN2016,06SEP2016,29NOV2016,,90,90,90,90,
4,A,01JAN2016,01APR2016,13APR2016,31DEC2016,,90,90,90,30,
5,A,01JAN2016,01FEB2016,01MAR2016,01APR2016,,30,30,30,30,
5,B,01MAY2016,01JUN2016,01JUL2016,01AUG2016,,30,30,30,30,
;;;;
run;
*assume days_covered is a simple last therapy date minus first therapy date
since OP is unclear and provides no instructions for calculating days_covered
also use a brute force method assuming only 5 service dates since OP provides
no inoformation regarding how many possible service dates there might be;
data testing;
set testing;
days_covered = 0;
if serv_date2 then days_covered = serv_date2 - serv_date1;
if serv_date3 then days_covered = serv_date3 - serv_date1;
if serv_date4 then days_covered = serv_date4 - serv_date1;
if serv_date5 then days_covered = serv_date5 - serv_date1;
run;
proc sort data = testing;
by patid descending days_covered ;
run;
data testing;
set testing;
by patid;
class = 'Unknown ';
if first.patid then class = 'First Therapy';
else class = 'Add on';
if first.patid and last.patid then class = 'Mono';
keep patid therapy days_covered class;
run;
Output set is:
PATID
Therapy
days_covered
class
1
A
291
First Therapy
1
B
193
Add on
2
B
316
First Therapy
2
A
276
Add on
3
B
301
First Therapy
3
A
248
Add on
3
C
248
Add on
4
A
365
Mono
5
B
92
First Therapy
5
A
91
Add on
Edit:
You may need to sort by serv_date1 as well
by patid serv_date1 descending days_covered ;
This would be to cover situations where a therapy which had a larger days_covered number but started after a different therapy isn't classed as first therapy. Assumes the therapy with the earliest serv_date regardless of length is the first therapy, and one that started after, even if it ran longer, is an add on.
... View more