Hello! I am studying hospital admissions and readmissions and am trying to figure out how to concatenate two variables so that SAS creates the same value of the variable whether the patient was admitted first to hospital A then hospital B, or first to hospital B then A.
The best (clumsy) way I could come up with was to concatenate admission hospital-readmission hospital and then readmission hospital-admission hospital variables in different data sets, merge them, and then add their counts. The issue with this is that I am then double counting admissions. Thanks!
Sort them alphabetically first, assuming that names will be identical. Using that rule the two same hospitals should always appear in the same order. I also upcased them, and you may need compress/trim if the names are not 100% identical each time.
HospitalStart = upcase(Hospital1);
HospitalEnd = upcase(Hospital2);
call sortc(HospitalStart, HospitalEnd);
want = catx("-", HospitalStart, HospitalEnd);
You could probably nest those in some fashion but I spelled it out so it's clear how it works.
data have;
input admissionhospid $ readmissionhospid $ ;
cards;
A B
B A
A C
B C
;;;;
run;
data want;
set have;
HospitalStart = upcase(admissionhospid);
HospitalEnd = upcase(readmissionhospid);
call sortc(HospitalStart, HospitalEnd);
option1 = catx("-", HospitalStart, HospitalEnd);
if admissionhospid < readmissionhospid then option2 = catx("-", admissionhospid, readmissionhospid );
else option2 = catx("-", readmissionhospid, admissionhospid );
run;
Results:
Obs admissionhospid readmissionhospid HospitalStart HospitalEnd option1 option2 1 A B A B A-B A-B 2 B A A B A-B A-B 3 A C A C A-C A-C 4 B C B C B-C B-C
Can you show us a portion of the data you are using, and the desired output?
Yes--hope this snapshot helps. Each line of data is an admission-readmission dyad and I am trying to determine the number of admissions to each pair of hospitals. When I concatenate the admissionhospid and readmissionhospid and count their frequencies, 'AB' and 'BA' are counted separately, but I would like them to be counted together.
admissionhospid readmissionhospid
A B
B A
A C
B C
Sort them alphabetically first, assuming that names will be identical. Using that rule the two same hospitals should always appear in the same order. I also upcased them, and you may need compress/trim if the names are not 100% identical each time.
HospitalStart = upcase(Hospital1);
HospitalEnd = upcase(Hospital2);
call sortc(HospitalStart, HospitalEnd);
want = catx("-", HospitalStart, HospitalEnd);
You could probably nest those in some fashion but I spelled it out so it's clear how it works.
data have;
input admissionhospid $ readmissionhospid $ ;
cards;
A B
B A
A C
B C
;;;;
run;
data want;
set have;
HospitalStart = upcase(admissionhospid);
HospitalEnd = upcase(readmissionhospid);
call sortc(HospitalStart, HospitalEnd);
option1 = catx("-", HospitalStart, HospitalEnd);
if admissionhospid < readmissionhospid then option2 = catx("-", admissionhospid, readmissionhospid );
else option2 = catx("-", readmissionhospid, admissionhospid );
run;
Results:
Obs admissionhospid readmissionhospid HospitalStart HospitalEnd option1 option2 1 A B A B A-B A-B 2 B A A B A-B A-B 3 A C A C A-C A-C 4 B C B C B-C B-C
Readmission frequently happens more than once and admission may as well over time.
So you need to provide some pretty clear rules about which are initial admissions and which re-admissions might be associate with a specific admission.
I suspect that date information may be helpful but examples are needed to provide good workable approaches.
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.