【问题标题】:Conditional cumulative sum in SASSAS中的条件累积和
【发布时间】:2017-01-09 20:17:27
【问题描述】:

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

我在 SAS 中有下表。我需要在日期之前获得累积值。最重要的是,一旦超过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 
   ...           ...

我需要我的输出表看起来像这样。只有一列显示累积值。

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 步与 RETAIN 一起使用?此外,倒数第二行的累积应该是 4,而不是 3,这看起来像是一个错字。
  • 如果你发布一个更好的解释你想要什么,你肯定会得到答案。如果您更好地定义规则,这绝对是一个有人会帮助解决的难题。

标签: sas conditional cumulative-sum


【解决方案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;

应该可以...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多