【问题标题】:Create date between consecutive dates在连续日期之间创建日期
【发布时间】:2022-11-25 08:15:01
【问题描述】:

我希望你能提供帮助。

我有一个 SAS 数据集,它有两列,ID 和 Date,如下所示: 在某些情况下,日期列会跳过一个月。我需要一个代码来创建每个 ID 的缺失日期,例如对于 AY273,我需要一个创建日期为 2022/11/20 的代码,对于 WG163,我需要一个创建日期为 2022/12/15 的代码。

【问题讨论】:

  • 每个 ID 的月中日期部分是否始终不变?
  • 不,它可以变化但变化不大。例如。对于第一个 ID,日期可能在 18 - 22 之间变化。该日期是保费收集日期,因此它将基于最近的工作日,但我需要创建的日期不必遵循该规则
  • 好的。因此,无论变化很小,在您的数据中,两个日期永远不会出现在同一个月内,对吗?
  • 是的,每个日期都有一个唯一的月份
  • 舒尔?你永远不会有人支付2022/01/302022/03/12022/03/30,......?

标签: date if-statement join sas uniqueidentifier


【解决方案1】:

尝试这个

data WANT (drop = this_date last_date);
    set HAVE(rename=(date = this_date));
    by id;
    last_date = lag(this_date);
    if first.id then do;
        date = this_date;
        output;
    end;
    else do date = this_date to last_date + 16 by -30;
        output;
    end;

   format date yymmdd10.;
proc sort;
    by id date;
run;

如果它不起作用,我会更正它。

【讨论】:

    【解决方案2】:

    您可以将数据与其自身合并,向前移动一个观察值(以获得领先值)并在该范围内循环。

    例子:

    data have;
    input id $ date yymmdd10.;
    format date yymmdd10.;
    datalines;
    AAAAA 2021-11-20
    AY273 2022-10-20
    AY273 2022-12-20
    AY273 2023-01-20
    WG163 2022-10-15
    WG163 2022-11-15
    WG163 2023-01-15
    ZZZZZ 2022-01-15
    ;
    
    data want(keep=id date fillflag);
      merge have have(rename=(date=leaddate id=leadid) firstobs=2);
    
      if id eq leadid then
        do while (intck('month',date,leaddate) > 0);
          output;
    
          date = intnx('month',date,1,'sameday');
          fillflag = 1;
        end;
      else
        output;
    run;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2021-08-19
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多