SAS中的LOOP与日期

时间:2013-09-27 10:19:23

标签: sas

我之前发布了类似的循环问题。

在这里,我必须循环到2011年到2022年,但对于2011年,计算与2012年到2022年不同。对于2012年以来,cost_2012取决于cost_2011,cost_2013取决于2012年的成本..我尝试使用此代码但我收到错误信息。

%MACRO NFORE1;

proc sql;
create table  cost_news_&time  as

select  *
         ,case  (31DEC2011.d-EIS)/365<=10  then  Segment_0_10
                (31DEC2011.d-EIS)/365<=20  then  Segment_10_20
                 else note  end as cost_AGE_2011

,Latest_cost+('31DEC2011'd-Latest_cost_Date)/30.44*cost_AGE_2011 as cost_2011

%DO TIME=2012 %TO 2022;
%LET ltime=%eval(&time-1);

,case 
       ("31DEC&time."d-EIS)/365<=10  then  Segment_0_10
       ("31DEC&time."d-EIS)/365<=20  then  Segment_10_20
         else note  end as cost_AGE_&time

,case
     calculated cost_&ltime + calculated cost_AGE_&time * 12  as cost_&time

     from cost_news
;
quit;
%END;
%MEND NFORE1;
%NFORE1;

1 个答案:

答案 0 :(得分:0)

data cost_news;
length EIS Latest_cost Latest_cost_Date Segment_0_10 Segment_10_20 Note 8;
run;

options mprint;
%MACRO NFORE1;

proc sql;
create table  cost_news_  as

select  *
         ,case  when ("31DEC2011"d-EIS)/365<=10  then  Segment_0_10
                when ("31DEC2011"d-EIS)/365<=20  then  Segment_10_20
                 else note  end as cost_AGE_2011

,Latest_cost+('31DEC2011'd-Latest_cost_Date)/30.44* calculated cost_AGE_2011 as cost_2011

%DO TIME=2012 %TO 2022;
%LET ltime=%eval(&time-1);

,case 
       when ("31DEC&time"d-EIS)/365<=10  then  Segment_0_10
       when ("31DEC&time"d-EIS)/365<=20  then  Segment_10_20
         else note  end as cost_AGE_&time

, calculated cost_&ltime + calculated cost_AGE_&time * 12  as cost_&time

%END;
    from cost_news
;
quit;
%MEND NFORE1;
%NFORE1;

您的所有变量都是数字的吗?因为您以这种方式使用它们,包括NOTE。

我的变化:   - DO循环重复了FROM子句 - 你只需要一个,对吧?还有QUIT声明。

  • 31DEC2011.d更改为"31DEC2011"d

  • WHEN添加到CASE个词组

  • calculated关键字添加到cost_AGE_2011。

无论如何,datastep编程对此更加清晰。