I have a degree in statistics and I'm now working through the SAS base programmer prep paterials. I'm having a hard time interpreting the code in this chapter.
As follows:
data-work.earn(drop=month); set cert.master;
Earned=0;
earned+(amount+earned)*(rate/12);
earned+(amount+earned)*(rate/12);
earned+(amount+earned)*(rate/12);
(...)
earned+(amount+earned)*(rate/12);
run;
This code is supposed to calculate the total amount of money (the variable "Earned") at the end of the year given a monthly interest rate.
But why isn't "Earned" just zero in this case?
What in the code connects the variable "earned" to the expression "earned+(amount+earned)*(rate/12);"?
Those are not normal assignment statements, they are sum statements. Notice the lack of the equal sign that is required in an assignment statement.
Look up the meaning of the SUM statement.
I checked out the page but the explanation wasn't as detailed as I'd like.
What "makes" this into a SUM-statement?
Are unassigned expressions (i.e x+0,5y) automatically assigned to the last mentioned variable?
@Syntax wrote:
What "makes" this into a SUM-statement?
Maybe it helps you to think of the sum statement as one of the few exceptions where the syntax of a SAS statement does not contain its name. (Other examples include the assignment statement and the comment statement as opposed to DATA, DO, END, INFILE, etc.) This becomes apparent when you skim through the list of Base SAS statements and spot the few entries whose first word is not written in all uppercase.
The important differences between the sum statement (with numeric variables x and y)
x+y;
and the similar assignment statement
x=x+y;
are:
But what "makes" this into a SUM statement? Does SAS "typically" summarize expressions after a semicolon (that would be weird) or is there any kind of further restrictions (i.e it's only a SUM statement when somehing is set to zero and the succeeding expressions are not assigned (lacks an equal sign)).
A SUM statement means there is no equals sign and that the value is added to the first argument in the statement.
This is another example of the sum statement. See what happens on each line here and that there is an implicit RETAIN as well.
data want;
x = 100;
do i=1 to 100;
x+i;
output;
end;
run;
It's a SUM statement because it follows the valid syntax for that definition. There are other ways to do this, so if you don't like this method or find it confusing you always choose to not use it.
@Syntax wrote:
But what "makes" this into a SUM statement? Does SAS "typically" summarize expressions after a semicolon (that would be weird) or is there any kind of further restrictions (i.e it's only a SUM statement when somehing is set to zero and the succeeding expressions are not assigned (lacks an equal sign)).
The type of statement is determined by the syntax you use. A DATA statement starts with the keyword DATA. An assignment statement is of the form:
var = expression ;
A sum statement is of the form:
var + expression ;
And where is the "iterative do loop" that is mentioned in your subject?
It's the name of the chapter in which this code is found.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.