sum 和 + 之间的 sas 区别?

问题描述:

data whatever;
 infile '';
 input price cost;
 <statement>;
run;

中,使用 total = sum(total,cost)total = total + cost 有什么区别?

In <statement>, what's the difference between using total = sum(total,cost) and total = total + cost ?

区别如下:

如果你想计算累计总数,你应该使用 sum 语句.

If you want to calculate cumulative total , you should use sum statement.

total = sum(total,cost)/* 它是一个求和函数 */

total = sum(total,cost) /* its a sum function */

total+cost/* 是一个 sum 语句,形式为变量+表达式 */

total+cost /* its a sum statement,the form is variable+expression */

这里:

"total" 指定累加器变量的名称,其中包含一个数值.

"total" specifies the name of the accumulator variable, which contains a numeric value.

1) 在 SAS 读取第一个观察值之前,变量(在本例中为总计)自动设置为 0.变量的值从一次迭代到下一次迭代都会保留,就像它出现在 RETAIN 语句中一样.

1) The variable(total in this case) is automatically set to 0 before SAS reads the first observation. The variable's value is retained from one iteration to the next, as if it had appeared in a RETAIN statement.

2) 要将 sum 变量初始化为 0 以外的值,请将其包含在具有初始值的 RETAIN 语句中.

2) To initialize a sum variable to a value other than 0, include it in a RETAIN statement with an initial value.

成本"是一个表达式

1) 计算表达式并将结果添加到累加器变量中.

1) The expression is evaluated and the result added to the accumulator variable.

2) SAS 将产生缺失值的表达式视为零.

2) SAS treats an expression that produces a missing value as zero.

sum 语句与 sum 函数的不同之处在于 sum 语句保留它之前计算出的值.

A sum statement is differ from a sum function in a way that a sum statement retains the value which it has calculated earlier.

然而,sum 语句等效于使用 SUM 函数和 RETAIN 语句,如下所示:

However, The sum statement is equivalent to using the SUM function and the RETAIN statement, as shown here:

retain total 0;
total=sum(total,cost);