【问题标题】:SAS - matching and assigning matchid to the matching recordSAS - 匹配并将 matchid 分配给匹配记录
【发布时间】:2022-12-16 10:07:26
【问题描述】:

我有如下数据,一行代表一个人,可以有一个或多个年龄相同且来自同一地区的人。我有一个包含案例的数据集和一个包含可能控件的数据集,它们看起来像这样:

data cases;
input id age area matchid;
datalines;
0101 25 12 1 
0120 25 12 2 
0125 30 11 3 
0130 45 11 4 
0135 45 11 5 
;

data controls;
input id age area;
datalines;
0203 25 12 
0250 26 12 
0320 25 12 
1023 30 11 
3020 45 11 
2036 45 11 
3022 46 11 
0204 25 12 
0321 25 12 
1025 30 11 
3026 45 11 
2070 45 11 
;

她是我的代码:

data control; set control;
check=1;
proc sort; by check; run;

data cohort; set case(where=(matchid=1)); 
   matchid=0; 
run;

%macro ccloop;
   %do i=1 %to &nobs;
data nowcase; set case(where=(matchid=&i)) end=final;
         case_id=id;
         case_age=age;
         case_area=area;
         check=1;
         drop id age area;
      proc sort data=nowcase; by check;
      data nowcase; merge nowcase control; by check; 
         if case_age=age and case_area=area;
         zzz=ranuni((667+&i));
      proc sort data=nowcase; by zzz; run;
      data nowcase; set nowcase; if _N_<=10; run;
      data casecase; set nowcase; 
         id=case_id;
         CASE=1;
         age=case_age;
         area=case_area;
         keep id case age area matchid;
      data casecont; set nowcase;
         CASE=0;
         keep id case age area matchid;
      data cohort; set cohort casecase casecont;
    dm log "clear";
   %end;
   data cohort; set cohort; if matchid=0 then delete;

%mend ccloop; run;

%ccloop; run;

提前致谢 :)

我想为每个案例找到 10 个 unik 控件,并为控件分配新数据集 case=0,为案例分配 case=1,我希望控件从它们匹配的案例中获取相应的 matchid。我的代码的问题是最终数据集中有重复项,我需要帮助来调整代码以避免这种情况或一些提示以另一种方式来做到这一点。

【问题讨论】:

  • 使用数据步骤代码进行编辑。鉴于此数据,您想要的结果是什么样的?
  • 欢迎来到 StackOverflow。您的预期输出是什么?宏变量nobs 解析为什么?

标签: sas matching


【解决方案1】:

谢谢:) 彼得和克米特:

最终的数据集应该是这样的:

id age area matchid case
0101 25 12 1 1
0203 25 12 1 0
0120 25 12 2 1
0320 25 12 2 0
0125 30 11 3 1
1025 30 11 3 0
0130 45 11 4 1
3026 45 11 4 0
0135 45 11 5 1
2070 45 11 5 0

这是一个例子,每个案例只有一个控件,我想每个案例有 10 个,我只希望一个控件被“使用”一次——我的主要问题是,即使我有足够的控件,相同的控件也会出现更多然后一次不同的情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多