按行累计

时间:2016-06-25 20:32:01

标签: sas data-manipulation

对于以下数据集

data example;
input x y;
cards;
1 8
2 7
3 6
4 5
;
run;

我想创建一个添加了以下列的新数据集:

sum 
10 
9 
7 
4

10来自第一列(1 + 2 + 3 + 4)的总和,9来自(2 + 3 + 4),依此类推。

1 个答案:

答案 0 :(得分:4)

使用proc sql

考虑相关的聚合子查询
proc sql;
    create table cumsum as
    (select sup.x, sup.y,
          (select sum(sub.x) from example as sub where sub.x >= sup.x) as sum 
     from example as sup);
quit;

x   y   sum
1   8   10
2   7   9
3   6   7
4   5   4

或者,您可以通过数据步骤运行累积和。由于您需要递减累积总和,因此需要先前和之后的排序:

proc sort data=example out=example;
    by descending x;
run;

data cumsum2;
  set example;
  if first.x then sum = 0;
  sum + x;    
run;

proc sort data=cumsum2 out=cumsum2;
    by x;
run;

x   y   sum
1   8   10
2   7   9
3   6   7
4   5   4