Below should work:
data date;
infile datalines delimiter = ',';
input ID $ DATE:mmddyy10. TYPE $11. PAIN HEART INPAT OUTPAT;
format date mmddyy10.;
datalines;
1, 05/03/2018, OUTPATIENT, ., 1, ., 1
1, 05/02/2018, OUTPATIENT, ., 1, ., 1
2, 01/26/2018, OUTPATIENT, 1, ., ., 1
2, 01/18/2018, OUTPATIENT, 1, ., ., 1
2, 01/10/2018, OUTPATIENT, 1, ., ., 1
;
run;
%macro split(cond);
%macro _;
%mend _;
proc sql;
select distinct put(date,monyy5.)
into :date1 - :date&sysmaxlong
from date;
%let cnt = &sqlobs;
quit;
/*Split the conditions*/
%do i = 1 %to &cnt;
%let d_start = %sysfunc(intnx(month,"01||&&date&i"d,0,b),date9.);
%let d_end = %sysfunc(intnx(year,"01||&&date&i"d,0,e),date9.);
%put &=d_start &=d_end;
proc sort data=date nodupkey out=&cond._&&date&i.;
by id date type;
where &cond.=1 and date >= "&d_start"d and date <= "&d_end"D;
run;
/*Determine what cases meet the case definition (2 outpatient or one inpatient unless is head trauma. Note:
head trauma is counted once regardless of inpatient or outpatient. The look back period is one year.*/
data &cond._&&date&i.._2;
set &cond._&&date&i.;
retain count;
by id date type;
if first.id then
count= 1;
else count = count + 1;/*adding the total appointments*/
%if &cond. ne HT %then
%do;
if last.id and count = 1 and INPAT = . then
delete;
%end;
run;
%end;
%mend split;
%split (PAIN);
... View more