Jinhua Wang
Jinhua Wang

Reputation: 1759

SAS Sum Syntax Issue

I am getting confused of the syntax SAS uses when sums across the column.

I wrote the following code to sum across the columns:

DATA SUM_RESULTS_ADF;
    SET VOLUME_DOLLAR;
    by SYM_ROOT;
    if %upcase(EX) = 'D';
    if first.SYM_ROOT then 
        do;
            SUMMED_DOLLARSIZE=0;
            SUMMED_SIZE=0;
        end;
    SUMMED_DOLLARSIZE + DOLLAR_SIZE;
    SUMMED_SIZE + SIZE;
    if last.SYM_ROOT then output;
    drop DOLLAR_SIZE SIZE;
RUN;

I just want to sum all the numbers in the column named DOLLAR_SIZE and size. But I am not sure if I am doing it correctly.

Because in OOC languages, we usually write: SUMMED_DOLLARSIZE = SUMMED_DOLLARSIZE + DOLLAR_SIZE;

But it seems that SAS doesn't need the equal sign here.

Upvotes: 1

Views: 204

Answers (1)

Tom
Tom

Reputation: 51566

The use of the SUM statement or the SUM(,...) function will handle missing values differently than just using the + operator. With SUM the missing values are ignored, but with + they will generate a missing result.

You are using the SUM statement. That is is just a short cut to save some typing.

The SUM statement has the form:

variable + expression ;

It is equivalent to these two statements:

retain variable 0 ;
variable = sum(variable,expression);

If you used simple addition instead of the SUM(,...) function then any observations with missing values would result in the sum being missing.

Here is a worked example:

data want ;
  input cost ;
  sum1 + cost ;
  retain sum2 0;
  sum2 = sum(sum2,cost);
  retain sum3 0;
  sum3 = sum3 + cost;
cards;
10
20
.
30
;

enter image description here

Upvotes: 2

Related Questions