0

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

在这里,我必须循环 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;
4

1 回答 1

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。_

  • 添加WHENCASE表达式

  • 添加calculated向 cost_AGE_2011

无论如何,数据步编程会更干净。

于 2013-09-27T11:30:32.070 回答