BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mirisage
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
LarryWorley
Fluorite | Level 6

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

3 REPLIES 3
art297
Opal | Level 21

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.

LarryWorley
Fluorite | Level 6

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

Tom
Super User Tom
Super User

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.


sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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