[英]Cummulative sum of variable by a condition and ID on sas
I am trying to sum one variable as long as another remains constant.只要另一个变量保持不变,我就会尝试对一个变量求和。 I want to cumulative sum dur as long as a is constant.
只要 a 是常数,我想累积总和。 when a changes the sum restarts.
当 a 发生变化时,总和重新开始。 when a new id, the sum restarts.
当一个新的 id 时,总和重新开始。
enter image description here在此处输入图像描述
and I would like to do this: enter image description here我想这样做:在此处输入图像描述
Thanks谢谢
You can use a BY
statement to specify the variables whose different value combinations organize data rows into groups.您可以使用
BY
语句来指定其不同值组合将数据行组织成组的变量。 You are resetting an accumulated value at the start of each group and adding to the accumulator at each row in the group.您正在重置每个组开始时的累加值,并在组中的每一行添加累加器。 Use
retain
to maintain a new variables value between the DATA step implicit loop iterations.使用
retain
在 DATA 步隐式循环迭代之间维护一个新的变量值。 The SUM statement is a unique SAS feature for accumulating and retaining. SUM 语句是一个独特的 SAS 功能,用于累积和保留。
Example:例子:
data want;
set have;
by id a;
if first.a then mysum = 0;
mysum + dur;
run;
The SUM
statement is different than the SUM
function. SUM
语句不同于SUM
function。
<variable> + <expression>; * SUM statement, unique to SAS (not found in other languages);
can be thought of as可以认为是
retain <variable>;
<variable> = sum (<variable>, <expression>);
As far as I am concerned, you need to self-join your table with a ranked column.就我而言,您需要使用排名列自行加入您的表格。
It should be ranked by id
and a
columns.它应该按
id
和a
列进行排名。
FROM WORK.QUERY_FOR_STCKOVRFLW t1;
is the table you provided in the screenshot是您在屏幕截图中提供的表格
PROC SQL;
CREATE TABLE WORK.QUERY_FOR_STCKOVRFLW_0001 AS
SELECT t1.id,
t1.a,
t1.dur,
/* mono */
(monotonic()) AS mono
FROM WORK.QUERY_FOR_STCKOVRFLW t1;
QUIT;
PROC SORT
DATA=WORK.QUERY_FOR_STCKOVRFLW_0001
OUT=WORK.SORTTempTableSorted
;
BY id a;
RUN;
PROC RANK DATA = WORK.SORTTempTableSorted
TIES=MEAN
OUT=WORK.RANKRanked(LABEL="Rank Analysis for WORK.QUERY_FOR_STCKOVRFLW_0001");
BY id a;
VAR mono;
RANKS rank_mono ;
RUN; QUIT;
PROC SQL;
CREATE TABLE WORK.QUERY_FOR_RANKRANKED AS
SELECT t1.id,
t1.a,
t1.dur,
/* SUM_of_dur */
(SUM(t2.dur)) FORMAT=BEST12. AS SUM_of_dur
FROM WORK.RANKRANKED t1
LEFT JOIN WORK.RANKRANKED t2 ON (t1.id = t2.id) AND (t1.a = t2.a AND (t1.rank_mono >= t2.rank_mono ))
GROUP BY t1.id,
t1.a,
t1.dur;
QUIT;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.