【问题标题】:Conditional Merge in SAS data stepSAS 数据步骤中的条件合并
【发布时间】:2017-06-22 22:15:50
【问题描述】:

我有两张表,为了简单起见分别是:

表 1:

Date     ID
20170401  X
20170501  Y
20170601  Z

表2:

Date     ID
20170201  Z
20170301  Y
20170501  X

我想创建一个包含表 1 中所有内容的新表,除非 ID 出现在表 2 中的前一个日期。

表 1 和 2 的期望输出是:

Date    ID 
20170401 X 

这是我目前拥有的。我不确定将条件合并放在哪里:

data new; 
merge table1 table2(in=b); 
by date ID; 
if not b [where table2.date is before table1.date]; 
run; 

谢谢

【问题讨论】:

    标签: sas


    【解决方案1】:

    不幸的是 SAS 会覆盖具有相同名称的变量,因此重命名 table2 中的日期。合并 ID 并仅在它在 table1 中并通过您的日期标准时保留。然后删除不需要的 date2 列。

    data new; 
      merge table1 (in=a) table2(in=b rename=(date=date2)); 
      by ID; 
      if a and not(date2<date);
      drop date2;
      run; 
    

    【讨论】:

    • 感谢您的帮助,但此解决方案是将 table1 中的 ID 与 table2 中不存在于 table1 中的日期合并。如果我只想要 table1 中的 ID 和 table1 中的日期,我该怎么做?谢谢。
    • 这更复杂,你想匹配 ID、日期然后排除一个 ID,如果它在 table2 中有 another 行的日期早于匹配的日期?
    猜你喜欢
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多