BookmarkSubscribeRSS Feed
cindyforest7
Calcite | Level 5


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!!!

2 REPLIES 2
PGStats
Opal | Level 21

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
slchen
Lapis Lazuli | Level 10

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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 906 views
  • 0 likes
  • 3 in conversation