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?
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:
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:
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.
Both create the same result, but the first is simpler and easier to read and understand, and therefore to be preferred.
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".
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:
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
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:
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:
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.