DATA Step, Macro, Functions and more

How to match tables based on different criteria

Reply
Contributor
Posts: 25

How to match tables based on different criteria


Hi,

I have two identical tables, and I want to match them together based on Age_grp, sex, and any_comorbidity.

Members are connected if they have same age_grp, or same sex, or same any_comorbidity. If two memebrs have three things in common, then the weight of the connection is 3; if two members have two things in common, then the weight of the connection is 2, and if two members only have one thing in common, then the weight should be 1.

A member cannot be connected to himself, and only connection with weight of at least of 1 should be shown.

Table 1

Member_id       Age_grp    sex     any_comorbidity

a                           1            F               Y

b                           1            M              Y

c                          2             M              N

d                          1             M              N

e                          3             F               N

f                           5             M              Y

g                          2             M               Y

h                          3             F               Y

Table 2

Member_id_match       Age_grp    sex     any_comorbidity

a                                         1            F               Y

b                                         1            M              Y

c                                         2             M              N

d                                         1             M              N

e                                         3             F               N

f                                          5             M              Y

g                                         2             M               Y

h                                         3             F              Y

I want a final table looks like this

Member_id      Member_id_match        weight

a                                 b                               2     

a                                 d                              1

a                                 e                               1

a                                 f                                 1

a                                g                                  1

a                                 h                                 2

....

....

....

Does any one have a solution here? Much appreciated!!!

Respected Advisor
Posts: 4,649

Re: How to match tables based on different criteria

Relatively simple with SQL :

data have;

input id    $   Age_grp    sex   $  any_c $;

datalines;

a                           1            F               Y

b                           1            M              Y

c                          2             M              N

d                          1             M              N

e                          3             F               N

f                           5             M              Y

g                          2             M               Y

h                          3             F               Y

;

proc sql;

create table match as

select

     a.id,

     b.id as match_id,

     (a.age_grp=b.age_grp) + (a.sex=b.sex) + (a.any_c=b.any_c) as weight

from

     have as a cross join

     have as b

where a.id < b.id and calculated weight > 0;

quit;

PG

PG
Super Contributor
Posts: 275

Re: How to match tables based on different criteria

data want;

set have;

do point=1 to nobs;

    set have (rename=(id=_id age_grp=_age_grp sex=_sex any_c=_any_c)) point=point nobs=nobs;

  if point>_n_ and id^=_id then do;

   weigth=sum(age_grp=_age_grp,sex=_sex,any_c=_any_c);

   if weigth>0 then output;

  end;

end;

keep id _id weigth;

run;

Shenglin

Ask a Question
Discussion stats
  • 2 replies
  • 183 views
  • 0 likes
  • 3 in conversation