在一个数据步骤中创建计算变量
Creating calculated variables in one datastep
非常感谢任何帮助。谢谢
我想用我的交易数据创建几个变量
我正在尝试使用数量、类型和 Op_Bal 创建变量 'act_bal' 和 'penalty'。我的规则是:
- 第一条记录id为op_bal
对于 type=C 从 'amount' 中减去,如果 type=D 则添加到
计算 act_bal
- 从第二条记录开始,它是 act_bal + type=C 的数量和
act_bal-类型数量=D
- 只有金额>4,类型=D,我才会加罚10。
id只能有两个penalty.
- 应从最后一个 act_bal 中减去总罚金
第二天的记录将变为 op_bal。 (例如,对于 id
101,-178-20=-198 将成为 4/2/2019 的 op_bal)
这是我在两个不同日期的两个客户 ID 101 和 102 的数据(我的实际数据集包含所有 30 天的数据)。
id date amount type Op_Bal
101 4/1/2019 50 C 100
101 4/1/2019 25 D
101 4/1/2019 75 D
101 4/1/2019 3 D
101 4/1/2019 75 D
101 4/1/2019 75 D
101 4/2/2019 100 C
101 4/2/2019 125 D
101 4/2/2019 150 D
102 4/1/2019 50 C 125
102 4/1/2019 125 C
102 4/2/2019 250 D
102 4/2/2019 10 D
我写的代码是这样的
data want;
set have;
by id date;
if first.id or first.date then do;
if first.id then do;
if type='C' then act_bal=Op_Bal - amount;
if type='D' then act_bal=Op_Bal + amount;
end;
else do;
retain act_bal;
if type='C' then act_bal=act_bal + amount;
if type='D' then act_bal=act_bal - amount;
if amount>4 and type='D' then do;
penalty=10;
end;
run;
我无法创建一个计数器来将惩罚控制为 2,也无法从最后一行的金额中减去总惩罚金额。有人可以帮我收到想要的结果吗?谢谢
id date amount type Op_Bal act_bal penalty
101 4/1/2019 50 C 200 150 0
101 4/1/2019 25 D 125 0
101 4/1/2019 150 D -25 10
101 4/1/2019 75 D -100 10
101 4/1/2019 3 D -103 0
101 4/1/2019 75 D -178 0
101 4/2/2019 100 C -198 -98 0
101 4/2/2019 125 D -223 10
101 4/2/2019 150 D -373 10
102 4/1/2019 50 C 125 175 0
102 4/1/2019 125 C 300 0
102 4/2/2019 250 D 50 0
102 4/2/2019 10 D 40 0
一些提示:
- 在
if
和 else
块中递增 act_bal
的代码相同,因此将其分解。不要重复自己。
- 如果使用 sum 语句,则可以跳过 retain 语句。
- 使用一个单独的变量来跟踪每天触发的处罚次数,但只应用其中的前两个。
所以,把它们放在一起:
data want;
set have;
by id date;
if first.date and not first.id then op_bal = act_bal;
if first.date then do;
act_bal = op_bal;
penalties = 0;
end;
if type='C' then act_bal + amount;
if type='D' then act_bal + (-amount);
if amount > 4 and type='D' then penalties + 1;
if last.date then act_bal + (-min(penalties,2) * 10);
run;
非常感谢任何帮助。谢谢
我想用我的交易数据创建几个变量
我正在尝试使用数量、类型和 Op_Bal 创建变量 'act_bal' 和 'penalty'。我的规则是:
- 第一条记录id为op_bal 对于 type=C 从 'amount' 中减去,如果 type=D 则添加到 计算 act_bal
- 从第二条记录开始,它是 act_bal + type=C 的数量和 act_bal-类型数量=D
- 只有金额>4,类型=D,我才会加罚10。 id只能有两个penalty.
- 应从最后一个 act_bal 中减去总罚金 第二天的记录将变为 op_bal。 (例如,对于 id 101,-178-20=-198 将成为 4/2/2019 的 op_bal)
这是我在两个不同日期的两个客户 ID 101 和 102 的数据(我的实际数据集包含所有 30 天的数据)。
id date amount type Op_Bal
101 4/1/2019 50 C 100
101 4/1/2019 25 D
101 4/1/2019 75 D
101 4/1/2019 3 D
101 4/1/2019 75 D
101 4/1/2019 75 D
101 4/2/2019 100 C
101 4/2/2019 125 D
101 4/2/2019 150 D
102 4/1/2019 50 C 125
102 4/1/2019 125 C
102 4/2/2019 250 D
102 4/2/2019 10 D
我写的代码是这样的
data want;
set have;
by id date;
if first.id or first.date then do;
if first.id then do;
if type='C' then act_bal=Op_Bal - amount;
if type='D' then act_bal=Op_Bal + amount;
end;
else do;
retain act_bal;
if type='C' then act_bal=act_bal + amount;
if type='D' then act_bal=act_bal - amount;
if amount>4 and type='D' then do;
penalty=10;
end;
run;
我无法创建一个计数器来将惩罚控制为 2,也无法从最后一行的金额中减去总惩罚金额。有人可以帮我收到想要的结果吗?谢谢
id date amount type Op_Bal act_bal penalty
101 4/1/2019 50 C 200 150 0
101 4/1/2019 25 D 125 0
101 4/1/2019 150 D -25 10
101 4/1/2019 75 D -100 10
101 4/1/2019 3 D -103 0
101 4/1/2019 75 D -178 0
101 4/2/2019 100 C -198 -98 0
101 4/2/2019 125 D -223 10
101 4/2/2019 150 D -373 10
102 4/1/2019 50 C 125 175 0
102 4/1/2019 125 C 300 0
102 4/2/2019 250 D 50 0
102 4/2/2019 10 D 40 0
一些提示:
- 在
if
和else
块中递增act_bal
的代码相同,因此将其分解。不要重复自己。 - 如果使用 sum 语句,则可以跳过 retain 语句。
- 使用一个单独的变量来跟踪每天触发的处罚次数,但只应用其中的前两个。
所以,把它们放在一起:
data want;
set have;
by id date;
if first.date and not first.id then op_bal = act_bal;
if first.date then do;
act_bal = op_bal;
penalties = 0;
end;
if type='C' then act_bal + amount;
if type='D' then act_bal + (-amount);
if amount > 4 and type='D' then penalties + 1;
if last.date then act_bal + (-min(penalties,2) * 10);
run;