【发布时间】:2018-05-05 19:15:15
【问题描述】:
【问题讨论】:
-
你尝试了什么?
标签: sas
【问题讨论】:
标签: sas
使用 sum 语句
data have;
input val;
datalines;
1
2
3
;
data want;
set have;
newval+val;
run;
【讨论】:
SUM 语句是 SAS 独有的语言元素。 SUM 语句<variable> + <expression>; 等价于retain <variable> 0; <variable> = sum ( <variable>, <expression> ); support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/…
使用Retain 功能。
您可以重复使用以下代码作为任何迭代/累积计算的基础。
data have;
input A;
datalines;
1
2
3
;
run;
data want;
set have;
Retain B;
/* If condition to initialize B only once, _N_ is the current row number */
if _N_= 1 then B=0;
B=B+A;
/* put statement will print the table in the log */
put _all_;
run;
输出:
A=1 B=1 _N_=1
A=2 B=3 _N_=2
A=3 B=6 _N_=3
【讨论】: