@serena13lee Very late I realised that may be you are interested in how many days a patient skipped the medicines and how many days a patient took medicines (irrespective of which medicine was taken or skipped). So I thought may be you wanted a table like below. Output table So after a bit of workaround and a bit of time investment, I got the code as below. I am sure there is an easier way to accomplish the same task. DATA Want ;
SET have;
BY ID medication;
RETAIN LowerBoundary 0;
RETAIN UpperBoundary 0;
RETAIN GapDays 0;
IF FIRST.ID AND LAST.ID THEN DO;
LowerBoundary = MIN(start_date, end_date);
UpperBoundary = MAX(start_date, end_date);
Cummulative = INTCK('day',LowerBoundary,UpperBoundary)+1;
Gapdays = 0;
OUTPUT;
LowerBoundary = 0;
UpperBoundary = 0;
END;
ELSE IF FIRST.ID AND NOT LAST.ID THEN DO;
LowerBoundary = MIN(start_date, end_date);
UpperBoundary = MAX(start_date, end_date);
Cummulative = .;
Gapdays = 0;
OUTPUT;
END;
ELSE IF NOT FIRST.ID AND LAST.ID THEN DO;
IF start_date > UpperBoundary THEN GapDays + INTCK('day',UpperBoundary,start_date) - 1;
IF MIN(start_date, end_date) <= LowerBoundary THEN LowerBoundary = MIN(start_date, end_date);
IF MAX(start_date, end_date) >= UpperBoundary THEN UpperBoundary = MAX(start_date, end_date);
Cummulative = INTCK('day',LowerBoundary,UpperBoundary)+1 - GapDays;
OUTPUT;
LowerBoundary = 0;
UpperBoundary = 0;
END;
ELSE DO;
IF start_date > UpperBoundary THEN GapDays + INTCK('day',start_date,UpperBoundary) - 1;
IF MIN(start_date, end_date) <= LowerBoundary THEN LowerBoundary = MIN(start_date, end_date);
IF MAX(start_date, end_date) >= UpperBoundary THEN UpperBoundary = MAX(start_date, end_date);
Cummulative = .;
OUTPUT;
END;
RUN;
PROC REPORT DATA=Want;
COLUMNS ID GapDays Cummulative;
DEFINE ID / GROUP "Patient ID" CENTER;
DEFINE GapDays / ANALYSIS SUM "Number of Days medicine skipped" CENTER;
DEFINE Cummulative / ANALYSIS SUM "Total Number of days medicine taken" CENTER;
RUN; Please let me know, when you can, if this was what you wanted.
... View more