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
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
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.
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
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.