> Hi all, I'm currently learning Basic SAS and am
> trying to learn the mechanics of DO Loops. I have a
> simple code here but would like to understand how SAS
> would execute it. Here is the code:
>
> data balance;
> bal=1000;
> do i=1 to 12;
> bal=bal*1.01;
> output;
> end;
>
> Although I do know what the result would be, I still
> have a few questions that I would like to ask that
> would better help me understand how SAS executes this
> loop.
>
> 1. assume SAS is reading this for the first time and
> is on its first loop (i=1). Since we're given that
> bal=1000 outside the DO Loop, how come IN the Do
> Loop, SAS will calculate it as bal = 1000*1.01 and
> not 1000 = 1000*10.1? Shouldn't the variable be
> different say total=bal*1.01?
>
Things on the LEFT side of a = sign are where results go (generally, there are exceptions but don't apply to this example). So BAL= in effect says "do what's on the right side of the equal sign and store the result in a place named BAL. If BAL already has a value it will be replaced. If you don't want to replace the value then don't. In your example of TOTAL=BAL*10.1; however you would get the exact same value for TOTAL. My take on the original code is someone wants to look at something like compound interest applying 1% successively.
Try this code to see what's happenng.
PROC Print data=balance; run;
> 2. so after SAS goes through the loop once, the
> balance after 1000 is multiplied by 1.01 and gives
> 1010, so how does SAS memorize the 1010 for the 2nd
> loop execution and multiply that by 1.01? Does SAS
> change the bal=1000 outside the DO Loop to bal=1010?
> If not, then how is SAS able to memorize the result
> from the previous loop and carry it into the next
> loop for calculation?
>
> Hope my questions are not confusing...thanks!
Conceptually BAL is a single storage bucket that only ever contains one value at a time within a single data step.