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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 357 views
  • 0 likes
  • 4 in conversation