BookmarkSubscribeRSS Feed
ysk
Calcite | Level 5 ysk
Calcite | Level 5

Hi there,

 

I am trying to merge two data tables (X and Y) by linking on multiple variables (e.g., age and gender to find matching cases)

 

Table X 

Participant ID Age Gender

Table Y

Encounter# Age Gender

 

I did a left join and output shows that some participant IDs are matched up with same encounter #s since the age and gender are equivalent. 

 

I'm hoping to retain unique encounter #s so if an encounter# is matched with participant ID it will not show up again with other participant ID.

 

How could I do this? 

3 REPLIES 3
Reeza
Super User

You can't with SQL but you can with other approaches. Greedy matching?

 

Mayo Clinic has a macro:

https://bioinformaticstools.mayo.edu/research/gmatch/

 

And there's PROC PSMATCH for propensity score matching.

 


@ysk wrote:

Hi there,

 

I am trying to merge two data tables (X and Y) by linking on multiple variables (e.g., age and gender to find matching cases)

 

Table X 

Participant ID Age Gender

Table Y

Encounter# Age Gender

 

I did a left join and output shows that some participant IDs are matched up with same encounter #s since the age and gender are equivalent. 

 

I'm hoping to retain unique encounter #s so if an encounter# is matched with participant ID it will not show up again with other participant ID.

 

How could I do this? 


 

s_lassen
Meteorite | Level 14

I think it can be done with SQL, something like this:

  select 
    x.*,uniq.encounter from
    x left join (
      select x.*,y.encounter
      from x join y on x.age=y.age and x.sex=y.sex
      group by y.encounter
      having x.participant_id=min(x.participant_id)
   ) uniq on x.participant_id=uniq.participant_id;
Ksharp
Super User

You want this ?

 

data x;
input id age sex;
cards;
1 19 1
1 20 0
1 40 0
2 19 1
2 20 0
;

data y;
input id age1 sex1;
cards;
1 19 1
1 20 0
2 19 1
2 20 0
2 22 1
;

data want;
 merge x y;
 by id;
 output;
 call missing(of _all_);
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 430 views
  • 0 likes
  • 4 in conversation