我想生成一个可以从现有列派生的新列,如下所示:
新列的第 1 行是给定列的前 21 行中所有条目的总和。
第 2 行又是接下来 21 行中所有条目的总和(从已给定列的第 2 行开始)。
等等。
小例子:
GivenColumn NewColumn
-----------------------------
1 15
2 14
3 12
4 9
5 5
有没有简单的方法可以做到这一点?在 Python 中,我将循环遍历所有行,从而达到我想要的结果。
谢谢!
试试这个
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