简体   繁体   English

在同一表中匹配观察值

[英]Matching observations in the same table

I was wondering if SAS can match observations in the same table based on multiple matching conditions and then tag them. 我想知道SAS是否可以根据多个匹配条件匹配同一表中的观测值,然后对其进行标记。 For instance I want observations which matches on age and gender but only differs based on if they like star wars or not. 例如,我希望观察的结果与年龄和性别相匹配,但仅取决于它们是否喜欢星球大战。

This is the data I have 这是我的数据

   age  gender  Like star wars  Location
    34  male         1            US
    36  female       0            UK
    24  female       1            AU
    45  female       1            US
    34  male         0            CH
    36  female       1            US
    57  female       0            US

Want

>    age    gender  Like star wars  Location    Match
>     34    male         1            US         Yes
>     36    female       0            UK         Yes
>     24    female       1            AU         No
>     45    female       1            US         No
>     34    male         0            CH         Yes
>     36    female       1            US         Yes
>     57    female       0            US         No

If you sort the data you have by age and gender, then the "matches" will be grouped together. 如果按年龄和性别对数据进行排序,则“匹配项”将分组在一起。 The non-matches can easily be detected with first. 首先可以很容易地检测出不匹配first. and last. last. processing. 处理。 Rows that are both first. 行都是first.first. and last. last. do not have a match. 没有比赛。

proc sort data=have;
by age gender;

data want;
set have;
by age gender;
match = ifc(first.age and last.age, 'No', 'Yes');
run;

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM