每月序列宏

时间:2017-12-19 15:04:13

标签: sql macros

我希望运行以下代码,以生成每月序列宏。下面的宏通过连接数据表中的月度文件来带来CASH_2PLUS和BALS_2PLUS的总和。

%macro codegen(yymm);
%local month i ;
%let month=%sysfunc(inputn(&yymm.01,yymmdd8));
PROC SQL;
CREATE TABLE TEAM_PERF_%sysfunc(intnx(month,&month,0),yymmn6) AS 
SELECT SUM(A.CASH_2PLUS) AS count_accts_0
%do i=1 %to 11 ;
,SUM(x&i..CASH_2PLUS) AS count_accts_&i
%end;
FROM data.cash_v1_%sysfunc(intnx(month,&month,0),yymmn6) A
%do i=1 %to 11 ;
LEFT JOIN data.cash_v1_%sysfunc(intnx(month,&month,&i),yymmn6) x&i
ON (A.HDR_ACCOUNT_ID= x&i..HDR_ACCOUNT_ID)
%end;
;
QUIT;
%mend codegen ;

%codegen (201612)

运行上述脚本时的结果如下所示

count_accts_0 count_accts_1 count_accts_2 .......count_accts_10 count_accts_11
    2004         1984        1975                     1543            1448

我希望下一个序列在此下添加记录:

count_accts_0 count_accts_1 count_accts_2 .......count_accts_10 count_accts_11
    2011         1999        1954                     1512            -

下一个序列

count_accts_0 count_accts_1 count_accts_2 .......count_accts_10 count_accts_11
    2043         2002        1999                     -            -

.
.
.
.

count_accts_0 count_accts_1 count_accts_2 .......count_accts_10 count_accts_11
    2078             -            -                     -            -

如果我按顺序调用宏,例如%codegen(201701)等等到201711,然后我需要%do循环以减量运行(例如1到10,1到9 ......所以直到它达到1)。基本上,我试图找出一个序列,%codegen(yyyymm)增加一个月,而%do循环减少1。

我是一个初学者,正在努力学习。

0 个答案:

没有答案