Hi,
I have a set of patients and their encounters at different hospitals. So, each patient may have visited different hospitals. So, I want to assign one hospital to each patient based on majority of their visits in the hospital. Some hospital info may be missing. For example-
Data I have:
Patient_ID |
Hospital |
1 |
A |
1 |
A |
1 |
C |
1 |
B |
1 |
. |
2 |
E |
2 |
F |
3 |
D |
3 |
F |
3 |
B |
3 |
B |
4 |
. |
4 |
. |
5 |
G |
5 |
G |
Data I want:
Patient_ID |
Hospital |
New_hospital |
FLAG |
1 |
A |
A |
|
1 |
A |
A |
|
1 |
C |
A |
|
1 |
B |
A |
|
1 |
. |
A |
|
2 |
E |
E |
1 |
2 |
F |
E |
1 |
3 |
D |
B |
|
3 |
F |
B |
|
3 |
B |
B |
|
3 |
B |
B |
|
4 |
. |
. |
|
4 |
. |
. |
|
5 |
G |
G |
|
5 |
G |
G |
|
Thank you very much for your support.
Please explain the exact logic behind when a patient should be flagged?
Check next code:
data have;
infile cards ;
input patient_ID Hospital $;
cards;
1 A
1 A
1 C
1 B
1 .
2 E
2 F
3 D
3 F
3 B
3 B
;run;
proc sql;
create table freq as
select patient_ID, Hospital,
count(hospital) as freq
from have
group by Patient_id, Hospital
order by Patient_ID, freq;
quit;
data flag;
set freq;
by Patient_ID;
if last.Patient_ID;
*drop freq; /* unmark to drop the frequency */
run;
data want;
merge have
flag(rename=(hospital=new_hospital));
by Patient_ID;
if Hospital= New_Hospital then flag=1;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.