Hi All,
Please help me in the programming for the below requirement.
Write a simple macro (SAS) to take an input dataset (sample rows below) as parameter and calculate the time duration (in days) for which a patient was prescribed a particular therapy.
Macro should have 2 parameter -input and output dataset
Sample
Input: Table : A Output data: Table: B
Pat Id | Drug | Prescription date |
|
| Pat Id | Drug | Days on treatment |
1001 | A | 1-Jan-22 |
|
| 1001 | A | 12 |
1001 | A | 12-Jan-22 |
|
| 1001 | B | 1 |
1001 | B | 1-Mar-22 |
|
| 1002 | C | 11 |
1002 | C | 4-Jan-22 |
|
|
| ||
1002 | C | 14-Jan-22 |
|
|
|
Note: Please assume a patient is continued on a particular therapy if there is next prescription available. Also ignore days’ supply calculation.
Try this
data have;
input PatId $ Drug $ Date :anydtdte.;
format Date date9.;
datalines;
1001 A 1-Jan-22
1001 A 12-Jan-22
1001 B 1-Mar-22
1002 C 4-Jan-22
1002 C 14-Jan-22
;
data want;
set have;
by PatId Drug;
if first.Drug then dt = Date;
if last.Drug then do;
days = Date - dt + 1;
output;
end;
retain dt;
drop Date dt;
run;
data have;
input PatId $ Drug $ Date :anydtdte.;
format Date date9.;
datalines;
1001 A 1-Jan-22
1001 A 12-Jan-22
1001 B 1-Mar-22
1002 C 4-Jan-22
1002 C 14-Jan-22
;
%macro xx(input=,output=);
proc sql;
create table &output. as
select PatId,Drug,range(Date)+1 as Days_on_treatment
from &input.
group by PatId,Drug;
quit;
%mend;
%xx(input=have,output=want)
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.