【问题标题】:In SAS how to check whether the date in a data set exists in the range of dates in another data set在SAS中如何检查一个数据集中的日期是否存在于另一个数据集中的日期范围内
【发布时间】:2017-10-26 11:31:52
【问题描述】:

我有两个数据集 Set1 和 Set2。

Set1 数据集有列 Curr_Dt:-

Set1
Curr_Dt
23/04/1998
01/01/2017
01/12/2018
10/10/2010

Set2 数据集有 3 列 St_Dt、End_Dt、Ind

  St_Dt              End_Dt               Ind
  01/11/2018        31/12/2018             N
  01/01/1998        31/05/1998             N
  30/11/2016        02/02/2017             N 

如果 Set1 的 Curr_Dt 落在 Set2 的 St_Dt 和 End_Dt 之间,我想将 Set2 数据集的 Ind 列更新为 Y。

【问题讨论】:

    标签: date merge sas set proc-sql


    【解决方案1】:

    我在这里没有看到要合并的键,所以我假设第一行与第一行一起,因此在每个数据集下。

    您可以通过一个简单的数据步骤来做到这一点。

    data want;
    merge set1 set2;
    
    if st_dt <= curr_dt <= end_dt then
       ind = 'Y';
    run;
    

    这也假设日期存储为日期而不是字符串。

    【讨论】:

      【解决方案2】:

      创建集合

      data Set1;
          length Curr_Dt $10;
          input Curr_Dt;
          cards;
      23/04/1998
      01/01/2017
      01/12/2018
      10/10/2010
      ;
      run;
      
      data Set2;
          length St_Dt $10 End_Dt $10 Ind $1;
          input  St_Dt$ End_Dt$ Ind$;
          cards;
      01/11/2018 31/12/2018 N
      01/01/1998 31/05/1998 N
      30/11/2016 02/02/2017 N 
      30/11/2005 02/02/2005 N 
      run;
      

      设置日期格式

      data Set1;
          set Set1;
          Curr = input(Curr_Dt, ddmmyy10.);
      run;
      
      data Set2;
          set Set2;
          St = input(St_Dt, ddmmyy10.);
          End = input(End_Dt, ddmmyy10.);
      run;
      

      如果 any Curr_Dt from Set1 介于 St_DtEnd_Dt 之间,则设置 Y 标志

      proc sql;
          create table Set2 as
          select distinct St_Dt, End_Dt,
          case when Set1.Curr>Set2.St and Set1.Curr<Set2.End
          then 'Y' else 'N' end as Ind
          from Set2
          left join Set1 on Set1.Curr>Set2.St and Set1.Curr<Set2.End;
      run;
      

      你得到

      01/01/1998  31/05/1998  Y
      30/11/2016  02/02/2017  Y
      01/11/2018  31/12/2018  Y
      30/11/2005  02/02/2005  N
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-17
        • 1970-01-01
        • 2019-12-27
        • 2019-04-26
        • 2019-05-20
        • 1970-01-01
        • 2018-05-22
        相关资源
        最近更新 更多