Sas 条件累积和

Sas 条件累积和,sas,Sas,我的问题是关于SAS中的条件累积和。我认为通过使用示例可以更好地解释这一点。我有以下数据集: Date Value 01/01/2001 10 02/01/2001 20 03/01/2001 30 04/01/2001 15 05/01/2001 25 06/01/2001 35 07/01/2001 20 08/01/2001

我的问题是关于SAS中的条件累积和。我认为通过使用示例可以更好地解释这一点。我有以下数据集:

Date                 Value
01/01/2001          10
02/01/2001          20
03/01/2001          30
04/01/2001          15
05/01/2001          25
06/01/2001          35
07/01/2001          20
08/01/2001          45
09/01/2001          35
我想找到值的累积和。我的条件是,如果累计总和超过70,则应为70,下一次累计总和应从超过70的过量值开始,以此类推。。更准确地说,我的新数据应该是:

Date                 Value    Cumulative 
01/01/2001           10        10
02/01/2001           20        30
03/01/2001           30        60
04/01/2001           15        70
05/01/2001           25        30 ( 75-70=5+25=30)
06/01/2001           35        65
07/01/2001           20        70
08/01/2001           45        60  ( 85-70=15+45=60)
09/01/2001           35        95   ( because its last value)

事先非常感谢

这里有一个解决方案,尽管肯定会有一个更优雅的解决方案。如果eof,则将其分成两部分,以满足最后的观测条件

data want;
  set test end = eof; 

  if eof ^= 1 then do;

    if cumulative = 70 then cumulative = extra;

    Cumulative + value;
    extra = cumulative - 70;

    if extra > 0 then do;
      cumulative = 70;
    end;
  end;

  retain extra;
  retain cumulative;

  if eof = 1 then cumulative + value;

run;

也许还有更优雅的,但我真的很喜欢你的。它还处理OP没有提到的条件,如remainders>70,因此额外的需要向下推到多行(可能到最后一行)。