## How this sum function works?

Solved
Super Contributor
Posts: 338

# How this sum function works?

Hi SAS Forum,

I have below data set.

data acct_li;

input old_lmt authorized_limit ;

cards;

700 1000

800 1500

5000 5000

100 110

7000 6000

;

run;

A previous worker has applied the follwoing code on the above data set.

data t_review1;

set acct_li ;

retain ActNum limitIcs 0;

if (authorized_limit>old_lmt)   then do;

ActNum+1;

limitIcs=sum(limitIcs, (authorized_limit-old_lmt));

output;

end;

run;

proc print;

run;

Above code produced the following output.

old_limit  authorized_limit   ActNum   limitIcs /*these are the table headings*/

 700 1000 1 300 800 1500 2 1000 100 110 3 1010

Q: Of the above code, I cannot understand how the below sum function portion is happening.

limitIcs=sum(limitIcs, (authorized_limit-old_lmt));

The red color "limitIcs" in the above sum function was never calculated before, but in the sum function we are asking the fucntion to add    limitIcs to (authorized_limit-old_lmt);

How come it happens?

Thanks

Mirisa

Accepted Solutions
Solution
‎04-14-2014 10:19 AM
PROC Star
Posts: 8,169

## Re: How this sum function works?

Yes, what I proposed is the sum statement.  If you look at the documentation ( SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition ), the sum statement is the equivalent of using the sum function together with a retain statement.  I misspoke when I referred to it as the sum function (which I will correct in my original response).

All Replies
PROC Star
Posts: 8,169

## Re: How this sum function works?

The sum function not only adds, but ignores missing values.

The code could also have been written:

limitIcs+(authorized_limit-old_lmt);

and have the same result since it is another way of accomplishing the same task as the sum function but, additionally, retains the summed variable without requiring a retain statement.

Super Contributor
Posts: 338

## Re: How this sum function works?

Hi Art,

Thank you very much for your inputs.

Yes, your alternate code which is

limitIcs+(authorized_limit-old_lmt); /*this looks like sum statement*/

produced the same results produced by

limitIcs=sum(limitIcs, (authorized_limit-old_lmt));    /* this is sum function*/

However, I am still confused (due to lack of my knowledge).

Green one is the "sum function" that was originally in the code I provided. You have changed it to the Yellow one which looks like "sum statement".

So, would this mean "sum function's" duty can also be achieved by "sum statement".

Thanks

Mirisa

Super User
Posts: 9,599

## Re: How this sum function works?

If you look at the SAS documentation on the sum function it adds up each value placed within the brackets separated by commas.  So in your example the brackets indicate that the code with should be done first, i.e. old_lmt value is taken off authorized_limit.  This is then added to limilcs.  So we could reword as:

limitlcs = add these items together  ( limitlcs and the result of authorized_limit minus old_lmt)

Solution
‎04-14-2014 10:19 AM
PROC Star
Posts: 8,169

## Re: How this sum function works?

Yes, what I proposed is the sum statement.  If you look at the documentation ( SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition ), the sum statement is the equivalent of using the sum function together with a retain statement.  I misspoke when I referred to it as the sum function (which I will correct in my original response).

Super Contributor
Posts: 338

## Re: How this sum function works?

Hi Art and RW9,

Many thanks to both of you.

Regards

Mirisa

🔒 This topic is solved and locked.