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

A quick question on simple investment with an annual interest of 3%:

data invest;

capital=2000;

do year =1 to 5;

capital +capital*0.03;

output;

end;

run;

 

someone codes as follows,

data invest2 (drop=count);

capital=2000;

do count=1 to 5;

capital+capital*0.03;

year+1;

output;

end;

run;

 

for me, they are the same. Why bother to induce a 'count' variable and then drop it while having a "year+1" statement?

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:

  If you move your PUT statement, you'd understand what is happening. There is a HUGE difference between an ASSIGNMENT statement which assigns a fixed value:

year = -2;

and a SUM statement which increments a retained value by the number after the operator. Remember that a SUM statement causes an initial value of 0 for the retained variable and then increments that variable by the number in the statement:

year + 1;

 

  In the first example, you are forcing an initial value of -2 for the value of YEAR, so the initial value is then incremented by 1 and -2+1 = -1 -- you broke the counter by starting the value of YEAR at -2, which is why the ending value is only 3. You can prove this to yourself by moving the PUT/PUTLOG statements:

Cynthia_sas_0-1606586041541.png

 

  On the other hand, if you take the default value used because of the SUM statement, the initial value of YEAR is 0, and incrementing by 1 for each time through the loop results in an ending value of 5, as shown below:

Cynthia_sas_1-1606586144711.png 

 

  Understanding the way the SUM statements works is how you understand why the value of YEAR is only 3 in your example.

 

Cynthia

 

 

  And you might want to revisit Programming 1 and Programming 2 so the difference is clear.

View solution in original post

8 REPLIES 8
anming
Pyrite | Level 9
thanks!
ballardw
Super User

Why do this?

Style choice.

 

The version with the year+1 was copied from somewhere and the results were as desired, so don't fix what isn't broken.

 

The version with the year+1 might have been edited from something with a Do Until or Do While loop control instead of iteration. Or there was other code in the do loop that was removed.

 

Following an instructors example that may be using that because the next lesson will use some feature that the other doesn't.

 

Lots of "why" possibilities. You would have to ask a specific programmer the reasons. Don't be surprised if the result is "didn't think of it".

anming
Pyrite | Level 9
Exactly as what you think, I am learning base SAS and wondering why the instructor codes like that rather than a simple one. I understand now. Thanks!
Cynthia_sas
SAS Super FREQ

Hi:

  In fact, the although the value of CAPITAL is the same after both loops, there is one value that is different at the end of each program, as highlighted below:

Cynthia_sas_0-1606504969027.png

We use programs like these to illustrate the point that in a DO loop like the first DO loop, the value of YEAR at the end of the program is the ending value+1, which is what causes the YEAR loop to end. And this shows in the second loop, where YEAR is incremented inside the COUNT loop, the last value of YEAR is 5 and the last value of COUNT is 6. Fully understanding how the DO LOOP works as far as what ends the loop is a basic concept that a student needs to understand before tackling DO WHILE and DO UNTIL.

 

Cynthia

anming
Pyrite | Level 9
Great! But why is "year" assigned to 1 in Loop 2?

I test the codes as follows;

data earning5;
year = -2;
capital = 2000,
do count = 1 to 5;
capital + capital*0.03;
year+1;
output;
end;
put _all_;
run;
the count is 6 and the year is 3. thanks!
Cynthia_sas
SAS Super FREQ

Hi:

  If you move your PUT statement, you'd understand what is happening. There is a HUGE difference between an ASSIGNMENT statement which assigns a fixed value:

year = -2;

and a SUM statement which increments a retained value by the number after the operator. Remember that a SUM statement causes an initial value of 0 for the retained variable and then increments that variable by the number in the statement:

year + 1;

 

  In the first example, you are forcing an initial value of -2 for the value of YEAR, so the initial value is then incremented by 1 and -2+1 = -1 -- you broke the counter by starting the value of YEAR at -2, which is why the ending value is only 3. You can prove this to yourself by moving the PUT/PUTLOG statements:

Cynthia_sas_0-1606586041541.png

 

  On the other hand, if you take the default value used because of the SUM statement, the initial value of YEAR is 0, and incrementing by 1 for each time through the loop results in an ending value of 5, as shown below:

Cynthia_sas_1-1606586144711.png 

 

  Understanding the way the SUM statements works is how you understand why the value of YEAR is only 3 in your example.

 

Cynthia

 

 

  And you might want to revisit Programming 1 and Programming 2 so the difference is clear.

anming
Pyrite | Level 9
really appreciated!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 8 replies
  • 1083 views
  • 2 likes
  • 4 in conversation