BookmarkSubscribeRSS Feed
SR11
Obsidian | Level 7

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-

  1. if a patient have 2, 3 and 4 visits in A, B and C hospital respectively, then C will be assigned to this patient as his hospital as the patient visited C for most of the times. 
  2. if a patient have 2, 4 and 4 visits in A, D and E hospital respectively, then D or E(any hospital) will be assigned to this patient as his hospital. But I want to “FLAG’ this patient.

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.

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Please explain the exact logic behind when a patient should be flagged?

SR11
Obsidian | Level 7
I am really sorry. I made a typo. I edited and corrected the post.
Here is my explanation for flagging a patient-
For Patient_ID 2, he visited hospital E and F equal times. So I chose any of the hospitals for New_Hospital(in the above example I chose E). And as he visited equal times in both hospitals I flagged it.
Thank you very much.
Shmuel
Garnet | Level 18

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1108 views
  • 0 likes
  • 3 in conversation