SAS 中的分区运行总计 [英] Partitioned Running Totals in SAS
问题描述
如何为现有数据集创建一个新列,该列是现有列的运行总计 - 由某个标识符分区?
How can I create a new column for an existing dataset that is the running total of an existing column - partitioned by some identifier?
ID | Value | New Value
---|--------|--------------------
1 | 10 | 10
1 | 5 | 15 = 10 + 5
1 | 3 | 18 = 10 + 5 + 3
2 | 45 | 45
2 | 15 | 60 = 45 + 15
我习惯于在 SQL (Oracle) 中使用简单的 SUM() OVER() 语句来完成此操作,但 PROC SQL 显然不支持该语法.
I'm used to accomplishing this in SQL (Oracle) with a simple SUM() OVER() statement, but that syntax is apparently not supported in PROC SQL.
如果可能,我想在 PROC SQL 中完成此任务(我在 SQL 方面的经验比 SAS 编码要丰富得多).
If possible, I would like to accomplish this within PROC SQL (I am much more experienced with SQL than SAS coding).
谢谢!
迈克.
推荐答案
Joe - 无论出于何种原因,你的答案都不起作用,但让我走上了正确的道路来解决这个问题.谢谢!
Joe - your answer did not work for whatever reason, but got me on the right track to figure it out. Thank you!
data want;
set have;
by id;
if first.id then running_total = 0;
if first.id then retained_total = 0;
running_total = retained_total + value;
retained_total = running_total;
retain retained_total;
run;
这篇关于SAS 中的分区运行总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!