【问题标题】:SAS: how to check if the same variable from two datasets are different?SAS:如何检查两个数据集中的相同变量是否不同?
【发布时间】:2017-07-08 02:17:00
【问题描述】:

我有两个数据集 DA 和 DB,都包含变量 ssn 并按 ssn 排序。如何检查 DA 是否包含一些未在 DB 中列出的 ssn,反之亦然?我考虑过 proc compare,但似乎比较过程只能比较同一位置的观察结果。例如,如果

data DA;
    input ssn &;
    datalines;
    100-00-0001
    100-00-0003
    100-00-0004
;


data DB;
    input ssn &;
    datalines;
    100-00-0001
    100-00-0002
    100-00-0003
;

然后比较结果将列出 100-00-0003 和 100-00-0004,尽管两个数据集都包含 100-00-0003。如何解决这个问题?

【问题讨论】:

    标签: sas


    【解决方案1】:

    通过SSN将两个数据集合并在一起,输出到不同的数据集,例如

    data A_only B_only;
      merge DA(in = a keep = SSN) 
            DB(in = b keep = SSN);
      by SSN;
      if a and not(b) then output a_only;
      if b and not(a) then output b_only;
    run;
    

    【讨论】:

      【解决方案2】:

      您可以在 PROC COMPARE 中使用ID 语句。未经测试:

      proc compare base=da compare=db listobs ;
        id SSN ;
      run ;
      

      还有输出到数据集的选项。

      【讨论】:

        【解决方案3】:

        使用proc sql:

        proc sql;
            select * from DA where ssn not in(select ssn from DB);
            select * from DB where ssn not in(select ssn from DA);
        quit;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-21
          • 1970-01-01
          • 2023-03-22
          • 2022-12-12
          • 2023-03-13
          • 2011-06-22
          • 2014-06-11
          相关资源
          最近更新 更多