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?
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?
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;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.