SAS 中的条件累计和
Conditional cumulative sum in SAS
我已经搜索了一段时间的解决方案,但我在社区中的 SAS 中找不到任何类似的问题。
我在 SAS 中有以下 table。我需要得到 Date 之前的累计值。最重要的是,我需要累计值一旦超过 10.
就从新开始
Date Number
01/01/2017 3
01/01/2017 1
01/01/2017 3
01/01/2017 4
01/01/2017 6
01/01/2017 8
02/01/2017 6
02/01/2017 3
02/01/2017 5
02/01/2017 7
03/01/2017 4
03/01/2017 3
... ...
我需要我的输出 table 看起来像这样。只有一列显示了累积值。
Date Number cumulative
01/01/2017 3 3
01/01/2017 1 4
01/01/2017 3 7
01/01/2017 4 4 <---- (starts from new)
01/01/2017 6 10
01/01/2017 8 8
02/01/2017 6 6
02/01/2017 3 9
02/01/2017 5 5
02/01/2017 7 7 <---- (starts from new)
03/01/2017 4 3
03/01/2017 3 7
... ... ...
是否有人可以协助解决这个问题。
谢谢
类似于(未测试):
data out;
set in;
by date; * Assumes sorted by date;
retain cumulative;
if first.date or cumulative+number > 10 then do;
cumulative = 0;
end;
cumulative = cumulative + number;
run;
应该可以...
我已经搜索了一段时间的解决方案,但我在社区中的 SAS 中找不到任何类似的问题。
我在 SAS 中有以下 table。我需要得到 Date 之前的累计值。最重要的是,我需要累计值一旦超过 10.
就从新开始Date Number
01/01/2017 3
01/01/2017 1
01/01/2017 3
01/01/2017 4
01/01/2017 6
01/01/2017 8
02/01/2017 6
02/01/2017 3
02/01/2017 5
02/01/2017 7
03/01/2017 4
03/01/2017 3
... ...
我需要我的输出 table 看起来像这样。只有一列显示了累积值。
Date Number cumulative
01/01/2017 3 3
01/01/2017 1 4
01/01/2017 3 7
01/01/2017 4 4 <---- (starts from new)
01/01/2017 6 10
01/01/2017 8 8
02/01/2017 6 6
02/01/2017 3 9
02/01/2017 5 5
02/01/2017 7 7 <---- (starts from new)
03/01/2017 4 3
03/01/2017 3 7
... ... ...
是否有人可以协助解决这个问题。
谢谢
类似于(未测试):
data out;
set in;
by date; * Assumes sorted by date;
retain cumulative;
if first.date or cumulative+number > 10 then do;
cumulative = 0;
end;
cumulative = cumulative + number;
run;
应该可以...