SAS:多行的总和并循环遍历所有行

问题描述 投票:0回答:1

我想生成一个可以从现有列派生的新列,如下所示:

新列的第 1 行是给定列的前 21 行中所有条目的总和。

第 2 行又是接下来 21 行中所有条目的总和(从已给定列的第 2 行开始)。

等等。

小例子:

 GivenColumn       NewColumn   
-----------------------------
  1                    15          
  2                    14        
  3                    12        
  4                     9        
  5                     5       

有没有简单的方法可以做到这一点?在 Python 中,我将循环遍历所有行,从而达到我想要的结果。

谢谢!

sas
1个回答
0
投票

试试这个

data have;
input GivenColumn;
datalines; 
1 
2 
3 
4 
5 
;

proc sql;
   create table want as
   select GivenColumn
        , (select sum(GivenColumn)
          from have as b 
          where a.GivenColumn <= b.GivenColumn)
          as NewColumn
   from have as a
   ;
quit;

结果:

GivenColumn NewColumn
1           15
2           14
3           12
4           9
5           5
© www.soinside.com 2019 - 2024. All rights reserved.