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
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 25. Read more here about why you should contribute and what is in it for you!
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.