Help using Base SAS procedures

How come SAS Accepts Sum = Sum (Sum, Age) ; ?

Accepted Solution Solved
Reply
Super Contributor
Posts: 338
Accepted Solution

How come SAS Accepts Sum = Sum (Sum, Age) ; ?

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
Frequent Contributor
Posts: 129

Re: How come SAS Accepts Sum = Sum (Sum, Age) ; ?

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

View solution in original post


All Replies
PROC Star
Posts: 7,492

Re: How come SAS Accepts Sum = Sum (Sum, Age) ; ?

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
Frequent Contributor
Posts: 129

Re: How come SAS Accepts Sum = Sum (Sum, Age) ; ?

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

Super User
Super User
Posts: 7,077

Re: How come SAS Accepts Sum = Sum (Sum, Age) ; ?

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.


🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 218 views
  • 6 likes
  • 4 in conversation