2017-01-09 93 views
0

我一直在寻找解决方案,但在社区中我找不到SAS中的任何类似问题。SAS中的条件累计总和

我在SAS中有下表。我需要通过日期获得累计值。最重要的是,我需要累计值从新开始,一旦超过。

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 
    ...   ... 

我需要我的输出表看起来像这样。再多一栏显示累计值。

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 
    ...   ...   ... 

有人能够协助这一点。

感谢

+0

请出示你已经尝试什么样的代码。您是否在RETAIN中使用DATA步骤?此外,看起来像一个错字,倒数第二行的累积数应该是4,而不是3. – Quentin

+0

如果您发布了更好的解释,那么您一定会得到答案。如果你更好地定义规则,这绝对是一个人会帮助你的难题。 –

回答

1

喜欢的东西(未经测试):

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; 

应该工作...