【问题标题】:Finding last Sunday and going 4 weeks backward every week in SAS在 SAS 中找到上周日并每周向后退 4 周
【发布时间】:2015-03-19 08:25:02
【问题描述】:

我有一个每周四运行的 SAS 作业,但有时它需要在星期三运行,也许是星期二晚上。该作业每隔 4 周收集一些数据,直到最近的星期日。例如,今天我们有 19Mar2015,我需要到 15Mar2015 的数据。

data get_some_data;
  set all_the_data;
  where date >= '16Feb2015' and date <= '15Mar2015';
run;

下周我也要手动更改日期参数

data get_some_data;
  set all_the_data;
  where date >= '23Feb2015' and date <= '22Mar2015';
run;

无论如何我可以自动执行此操作?

【问题讨论】:

  • 查找intnxtoday 函数的文档。

标签: date sas


【解决方案1】:

我将扩展 @user667489 的建议,因为您可能需要一段时间才能解决。关键是使用week 时间间隔,默认情况下从星期日开始(您可以使用班次索引更改此设置,阅读this 了解更多详情)

所以您的查询只需:

where intnx('week',today(),-4)<date<=intnx('week',today(),0);

【讨论】:

    【解决方案2】:

    使用INTNX 函数将日期回退到上周日:

    data get_some_data;
      set all_the_data;
      lastsun=intnx('week',today(),0);
      /*where date >= '23Feb2015' and date <= '22Mar2015';*/
      where date between lastsun-27 and lastsun;
    run;
    

    【讨论】:

      【解决方案3】:

      您可以尝试使用 weekday 函数获取最后一个星期日的日期,然后使用 INTNX 获取该星期日日期的 4 周回溯日期。检查以下参考代码:

          data mydata;
          input input_date YYMMDD10.;
          /* Loop to get the last sunday date, do the processing
             and get out of loop */
          do i =0 to 7 until(last_sunday_date>0);
              /* Weekday Sunday=1 */
              if weekday(sum(input_date,-i))=1 then do; 
                  last_sunday_date=sum(input_date,-i);  
                /* INTNX to get the 4 week back date */
                  my_4_week_start=intnx('week',last_sunday_date,-4); 
             end;
          end;
          format input_date last_sunday_date my_4_week_start yymmdd10.;
          datalines4;
          2015-03-01
          2015-03-07
          2015-03-14
          2015-03-21
          2015-03-28
          2015-04-05
          2015-04-13
          2015-04-20
          ;;;;
          run;
          proc print data=mydata;run;
      

      如果这有帮助,请告诉我!

      【讨论】:

        猜你喜欢
        • 2021-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-23
        相关资源
        最近更新 更多