BookmarkSubscribeRSS Feed
ankitasharma
Calcite | Level 5
Hi i ran the below codes and they gave me different outputs.i wanted to check how many years it will take for $100 at 3.75% int per annum to double?
For do:
Data x;
Int =0.375;
Total=100;
Do total= 100 to 200;
Year+1;
Total= total +int*total ;
Output; end;
Run; this gave 16 obs
While same query , changing do statement to do until(total ge 200)
The result changed to 19 obs.

Can someone explain what is the logic behind it?
Really appreciate your help.
2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hi @ankitasharma,

 

Only the DO-UNTIL approach is correct (assuming that you mean Int=0.0375). With the iterative DO loop (do Total=100 to 200;) Total is incremented by 1 at the end of each iteration (i.e. after the OUTPUT statement has executed), which is not what you want. You can replicate this behavior (just for demonstration) with the DO-UNTIL approach by inserting

 

total+1;

between the OUTPUT and END statements.

 

Related quote from the help file "DO Statement, Iterative":

CAUTION:
Avoid changing the index variable within the DO group.

 

Tom
Super User Tom
Super User

You are going to see a difference starting with the second time through the loop.

When you use DO UNTIL ( total ge 200) the value of total does not change from the end of one iteration of the loop to the start of the second.

But when you use DO TOTAL=100 to 200 (note that the increment amount will be 1 since it is not specified) the value of TOTAL is increased by 1 at the start of each new iteration.

 

Year Total In Total Out   Total In Total Out
1 100 103.75   100 103.75
2 103.75 107.6406   104.75 108.6781
3 107.6406 111.6771   109.6781 113.7911
4 111.6771 115.865   114.7911 119.0957

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

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 681 views
  • 0 likes
  • 3 in conversation