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

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);"?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
This is the SUM statement which has an implicit retained, and assigns the value to EARNED.

Earned is not 0, because AMOUNT is likely not 0.

this is equivalent to:

earned = earned + (amount + earned).....;

View solution in original post

9 REPLIES 9
Tom
Super User Tom
Super User

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.

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=lestmtsref&docsetTarget=n...

 

Syntax
Fluorite | Level 6

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?

FreelanceReinh
Jade | Level 19

@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:

  1. x is automatically initialized to zero (rather than missing).
  2. x is automatically RETAINed (i.e. not set to missing at the beginning of the next DATA step iteration).
  3. Missing values of y are treated as zeros.
Reeza
Super User
This is the SUM statement which has an implicit retained, and assigns the value to EARNED.

Earned is not 0, because AMOUNT is likely not 0.

this is equivalent to:

earned = earned + (amount + earned).....;
Syntax
Fluorite | Level 6

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)).  

Reeza
Super User

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;

https://documentation.sas.com/?docsetId=lestmtsref&docsetTarget=n1dfiqj146yi2cn1maeju9wo7ijs.htm&doc...

 

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.

Tom
Super User Tom
Super User

@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 ;
ballardw
Super User

And where is the "iterative do loop" that is mentioned in your subject?

Syntax
Fluorite | Level 6

It's the name of the chapter in which this code is found. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 9 replies
  • 1895 views
  • 4 likes
  • 5 in conversation