turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- How come SAS Accepts Sum = Sum (Sum, Age) ; ?

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-12-2012 11:51 AM

Hi Forum,

In this DOW Loop, could anyone please elaborate the logic of the equation highlighted by yellow?

In normal arithmetic I am familiar, these are logically correct:

Sum = Sum or Sum= Sum*1 or Sum = Sum +0 etc.

But SAS works really well when you say Sum = Sum (Sum, Age) but how come?

Data B ( Keep = Sex MCountSum Mean) ;

Do Until ( Last.Sex ) ;

Set Class ;

By Sex;

If Missing (Age) Then Continue ;

MCount = Sum (MCount, 1) ;

Sum = Sum (Sum, Age) ;

put _ALL_; /* look in the SAS LOG to see each iteration */

End;

Mean = Sum / MCount;

Run ;

Also I found in some other place

Total = Total + Interest * Total

In above case, the equation is true only if Interest = 0, isn’t it?

But SAS accepts Total = Total + Interest * Total

Thank you

Mirisage

Accepted Solutions

Solution

10-12-2012
12:33 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

10-12-2012 12:33 PM

Mirasage,

Art makes good point about sas allows the use of a function name for a variable name. Other programming languages might not let you do this.

But I think you also highlight another issue leading to confusion is that the equal sign is used both as a logical operater and to indicate an assign statement. Algebraic rules to not hold for both sides of an assignment statement. For example: the assignment statement Sum = Sum (Sum, Age) means the following:

1. Sum the previous value of the variable SUM and the variable Age

2. Take the results from 1 and replace the value of the variable SUM with that value.

On the other hand, if that expression where used as a logical expression, such as, 'IF Sum = Sum (Sum, Age)" would only be true if the value of Age is either missing or 0 (zero).

Larry

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Mirisage

10-12-2012 12:19 PM

Mirisage: SAS lets one use function names as variable names. The equations are true or false, they represent calculations.

When using the sum function, the result will only be missing if all of the parts are missing.

When one uses a statement like "Total = Total + Interest * Total" the result will be a missing value if ANY part is missing.

Solution

10-12-2012
12:33 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

10-12-2012 12:33 PM

Mirasage,

Art makes good point about sas allows the use of a function name for a variable name. Other programming languages might not let you do this.

But I think you also highlight another issue leading to confusion is that the equal sign is used both as a logical operater and to indicate an assign statement. Algebraic rules to not hold for both sides of an assignment statement. For example: the assignment statement Sum = Sum (Sum, Age) means the following:

1. Sum the previous value of the variable SUM and the variable Age

2. Take the results from 1 and replace the value of the variable SUM with that value.

On the other hand, if that expression where used as a logical expression, such as, 'IF Sum = Sum (Sum, Age)" would only be true if the value of Age is either missing or 0 (zero).

Larry

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Mirisage

10-12-2012 01:21 PM

Total = Total + Interest * Total ;

Is an assignment statement. So you wouldn't normally think of it as having a truth value.

It will add the calculated interest to the total variable. Mathematically equivalent to TOTAL = (1 + INTEREST) * TOTAL.

If INTEREST is missing then the new value of TOTAL will be missing.

If instead you use the SUM() function

TOTAL=sum(TOTAL,INTEREST*TOTAL);

or the sum operator

TOTAL+(INTEREST*TOTAL);

then missing values will be ignored.

Note that the sum operator will also mark the target variable (TOTAL) to be retained across data step iterations.