SAS LOOP中的日期功能

时间:2013-09-25 15:31:33

标签: sas sas-macro

我在EIS栏中有一个日期功能(EX:05FEB2007),我希望将年份从31DEC2012循环到31DEC2022,但是必须像31DEC2012-EIS一样循环到31DEC2022-EIS。

%MACRO NFORE;
   %LET UC=100;
   %LET YS=2012;
   %DO I = 0 %TO 10;
      %LET YRS=%EVAL(&YS+&I);
      proc sql;
         create table  FORECAST_&YRS  as
         select t.*,
            case when (31DEC&YRS-EIS)/365<=10 then  Segment_10
                 when (31DEC&YRS-EIS)/365<=20 then  Segment_20
                 when (31DEC&YRS-EIS)/365<=30 then  Segment_30
                 when (31DEC&YRS-EIS)/365<=99 then  Segment_35
                 else stat
            end as TSN_AGE_&YRS
         from F_AG t;
      quit;
   %END;
%MEND NFORE;
%NFORE;

1 个答案:

答案 0 :(得分:0)

我将以我的标准开头“这是一个坏主意,有一个更好的方法(你的问题)比在宏循环中产生大量数据集”,但是说:

%MACRO NFORE;
%LET  UC=100;
%LET YS=2012;

%DO yrs= &ys. %TO %eval(&ys.+10);

proc sql;
create table  FORECAST_&YRS  as
select  *
,case when ("31DEC&YRS."d-EIS)/365<=10 then  Segment_10
      when ("31DEC&YRS."d-EIS)/365<=20 then  Segment_20
      when ("31DEC&YRS."d-EIS)/365<=30 then  Segment_30
      when ("31DEC&YRS."d-EIS)/365<=99 then  Segment_35
      else stat  end as TSN_AGE_&YRS
from F_AG;
quit;
%END;
%MEND NFORE;
%NFORE;

在计算实际年份方面稍微优越的解决方案是使用INTCK函数来确定日期之间的年份,这将处理闰年 - 甚至在EIS上使用YEAR函数比较&amp; yrs。到年(EIS),因为你无论如何都在使用DEC 31。