汇总当前行及其前一行的变量

时间:2013-10-29 13:29:27

标签: sas

这是我的意见:

data a;
policy_no = 1;
X_NET_PREMIUM_AMT = 1;
X_NET_PREMIUM_V_AMT = 8;
output;
policy_no = 1;
X_NET_PREMIUM_AMT = 2;
X_NET_PREMIUM_V_AMT = 6;
output;
policy_no = 1;
X_NET_PREMIUM_AMT = 9;
X_NET_PREMIUM_V_AMT = 4;
output;
policy_no = 1;
X_NET_PREMIUM_AMT = 12;
X_NET_PREMIUM_V_AMT = 5;
output;
policy_no = 1;
X_NET_PREMIUM_AMT = 6;
X_NET_PREMIUM_V_AMT = 3;
output;
policy_no = 2;
X_NET_PREMIUM_AMT = 18;
X_NET_PREMIUM_V_AMT = 78;
output;
policy_no = 2;
X_NET_PREMIUM_AMT = 25;
X_NET_PREMIUM_V_AMT = 16;
output;
policy_no = 2;
X_NET_PREMIUM_AMT = 91;
X_NET_PREMIUM_V_AMT = 42;
output;
policy_no = 3;
X_NET_PREMIUM_AMT = 123;
X_NET_PREMIUM_V_AMT = 52;
output;
policy_no = 3;
X_NET_PREMIUM_AMT = 4;
X_NET_PREMIUM_V_AMT = 1;
output;
run;

我需要做的是每个policy_no:

X_NET_PREMIUM_AMT = X_NET_PREMIUM_AMT OF PREVIOUS ROW + X_NET_PREMIUM_V_AMT OF CURRENT ROW.

到目前为止 - 没问题。

让我难以做的是, 我需要更新X_NET_PREMIUM_AMT以进行下一次计算, 我会解释一下:

第1行(policy_no = 1):

X_NET_PREMIUM_AMT = 0(no previouse row) + 8 = 8;

第2行(policy_no = 1):

X_NET_PREMIUM_AMT = 8 + 6 = 14;

第3行(policy_no = 1):

X_NET_PREMIUM_AMT = 14 + 4 = 18;

依旧......

不幸的是, 我不知道怎么做......

会非常感谢你的帮助

2 个答案:

答案 0 :(得分:1)

您要找的是X_NET_PREMIUM_AMTpolicy_no的累积总和。因为覆盖原始变量不是一个好主意,我会将新变量命名为cumsum

data b;
set a;
by policy_no;
retain cumsum;
if first.policy_no then cumsum=X_NET_PREMIUM_V_AMT; else cumsum=X_NET_PREMIUM_V_AMT+cumsum;
run;

因此,当遇到其他policy_no cumsum时,X_NET_PREMIUM_V_AMT将与policy_no相同,对于此X_NET_PREMIUM_V_AMT+cumsum的其他行,它将为policy_no。对于此代码,您的数据必须按{{1}}排序。

答案 1 :(得分:0)

data out;
    set a;
    by policy_no;
OUT_X_NET_PREMIUM_AMT = sum(X_NET_PREMIUM_AMT, lag1(X_NET_PREMIUM_AMT));
OUT_X_NET_PREMIUM_V_AMT = sum(X_NET_PREMIUM_V_AMT, lag1(X_NET_PREMIUM_V_AMT));
    if last.policy_no then output;
run;

使用lag1()函数 - 返回变量的前一个值。

也许你的意思是最后3个值:

OUT_X_NET_PREMIUM_AMT = sum(X_NET_PREMIUM_AMT, lag1(X_NET_PREMIUM_AMT), lag2(X_NET_PREMIUM_AMT));
OUT_X_NET_PREMIUM_V_AMT = sum(X_NET_PREMIUM_V_AMT, lag1(X_NET_PREMIUM_V_AMT), lag2(X_NET_PREMIUM_V_AMT));