Hey all, I have a database with ID numbers and a variable called Type_Of_Visit. If any patient (denoted by variable ID) has a Type_Of_Visit as "inpatient" then I need to tag that ID as 1 and the remaining IDs without any inpatient as 0.
The issue is each ID can have multiple types of visits like Emergency, Outpatient, inpatient, multiple outpatient, multiple Emergency and multiple inpatient, and any combination of these, etc. All I want to do is to create a new variable Inpatient_Count if that ID has an inpatient visit at any time (it does not matter if the same ID has other types of visits). If that ID has at least one inpatient encounter, I want to tag all entries for that ID as 1 (in the new variable), and rest of the IDs without inpatient encounter as 0. Hope I clarified it! Please let me know if need further clarification.
Thanks for the help in advance!
Here is what I want:
ID Type_Of_Visit Inpatient_Count
1 inpatient 1
1 Emergency 1
1 Outpatient 1
2 inpatient 1
2 inpatient 1
2 inpatient 1
3 outpatient 0
3 outpatient 0
3 Emergency 0
4 Emergency 0
5 Outpatient 0
6 inpatient 1
6 Emergency 1
7 Outpatient 0
7 Outpatient 0
7 Emergency 0
7 Emergency 0
/* UNTESTED CODE */
data count_inpatient;
set have;
if upcase(type_of_visit)='INPATIENT' then new_inpatient_variable=1;
else new_inpatient_variable=0;
run;
proc summary nway data=count_inpatient;
class id;
var new_inpatient_variable;
output out=count_inpatient1 max=;
run;
data count_inpatient2;
merge count_inpatient(drop=new_inpatient_variable) count_inpatient1;
by id;
run;
/* UNTESTED CODE */
data count_inpatient;
set have;
if upcase(type_of_visit)='INPATIENT' then new_inpatient_variable=1;
else new_inpatient_variable=0;
run;
proc summary nway data=count_inpatient;
class id;
var new_inpatient_variable;
output out=count_inpatient1 max=;
run;
data count_inpatient2;
merge count_inpatient(drop=new_inpatient_variable) count_inpatient1;
by id;
run;
Thank you!
proc sort data=have out=inp nodupkey;
by ID Type_of_visit;
where upcase(TYPE_OF_VISIT)='INPATIENT';
run;
data get;
merge have inp;
by ID;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.