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