This is the array code, adapted to deal with the 30 days window and multiple "inpatient" encounters:
data want;
array visits {&start.:&end.} _temporary_;
set have;
by patient_id;
if first.patient_id
then do;
call missing(of visits{*});
newvar1 = .;
newvar2 = " ";
end;
visits{admtdate} = (encounter_type = "er");
if status = "died" then newvar2 = encounter_type;
if encounter_type = "inpatient"
then do;
do i = admtdate - 30 to admtdate - 1;
newvar1 + sum(0,visits{i});
end;
output;
call missing(of visits{*});
newvar1 = .;
newvar2 = "";
end;
if last.patient_id and (newvar1 ne . or newvar2 ne "")
then output;
retain
newvar2
;
keep patient_id newvar1 newvar2;
run;
... View more