0

这是我的输入:

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;

等等....

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

会感谢你的帮助

4

2 回答 2

1

您正在寻找的是X_NET_PREMIUM_AMTper的累积总和policy_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按此代码排序,这里就是这种情况。

于 2013-10-29T16:25:13.100 回答
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));
于 2013-10-29T13:36:58.333 回答