sas difference between sum and +?

后端 未结 2 1228
孤独总比滥情好
孤独总比滥情好 2021-01-24 08:49
data whatever;
 infile \'\';
 input price cost;
 ;
run;

In , what\'s the difference between using

2条回答
  •  时光取名叫无心
    2021-01-24 09:17

    The differnce can be seen below:

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

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

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

    Here :

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

    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) To initialize a sum variable to a value other than 0, include it in a RETAIN statement with an initial value.

    and

    "Cost" is an expression

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

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

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

    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);
    

提交回复
热议问题