【问题标题】:Filling in the months and years in date range?填写日期范围的月份和年份?
【发布时间】:2013-03-21 20:10:14
【问题描述】:

我是 SAS 新手,想知道如何最有效地列出开始日期和结束日期之间的月份和年份,以及开始日期和结束日期本身。我已经阅读了有关 INTCK 和 INTNX 函数、时间序列数据的 EXPAND 函数,甚至 CALENDAR FILL 的信息,但我不确定如何将它们用于此特定目的。由于具有下拉自动填充功能,此任务很容易在 Excel 中使用小型数据集手动完成,但由于数据集的大小,我需要在 SAS 中找到一种方法来执行此操作。任何建议将不胜感激。谢谢!

数据集在一个大文本文件中,现在组织如下:

ID          Start               End
1000        08/01/2012          12/31/2012
1001        07/01/2010          05/31/2011
1002        04/01/1990          10/31/1991

但最终的输出应该是这样的:

ID      MonthYear
1000    08/12
1000    09/12
1000    10/12
1000    11/12
1000    12/12
1001    07/10
1001    08/10
1001    09/10
1001    10/10
1001    11/10
1001    12/10
1001    01/11
1001    02/11
1001    03/11
1001    04/11
1001    05/11
1002    04/90
1002    05/90
1002    06/90
1002    07/90
1002    08/90
1002    09/90
1002    10/90
1002    11/90
1002    12/90
1002    01/91
1002    02/91
1002    03/91
1002    04/91
1002    05/91
1002    06/91
1002    07/91
1002    08/91
1002    09/91
1002    10/91

【问题讨论】:

    标签: sas time-series


    【解决方案1】:
    data want2;
      set have;
      do i = 0 to intck('month',start,end);
          monthyear=intnx('month',start,i,'b');
          output;
          end;
       format monthyear monyy.;
       keep id monthyear;
       run;
    

    【讨论】:

      【解决方案2】:

      这样就可以了。 PROC EXPAND 可能更有效,尽管我认为它需要一些期望的观察而不是开始/结束组合(尽管你可以得到,我想)。

      data have;
      informat start end MMDDYY10.;
      input ID          Start               End;
      datalines;
      1000        08/01/2012          12/31/2012
      1001        07/01/2010          05/31/2011
      1002        04/01/1990          10/31/1991
      ;;;;
      run;
      
      data want;
      set have;
      format monthyear MMYYS5.;    *formats the numeric monthyear variable with your desired format;
      monthyear=start;             *start with the initial observation;
      output;                      *output it;
      do _t = 1 by 1 until (month(monthyear)=month(end)); *iterate until end;
        monthyear = intnx('month',monthyear,1,'b');       *go to the next start of month;
        output;                                           *output it;
      end;
      run;
      

      【讨论】:

      • 这也有效,非常感谢@Joe!解释有帮助。
      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2012-12-14
      • 2013-11-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多