BookmarkSubscribeRSS Feed
KyleS83
Calcite | Level 5
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;
run;

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?

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!
4 REPLIES 4
ballardw
Super User
> 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.
art297
Opal | Level 21
Kyle,

I think you are asking "what does the program data vector (PDV) see?"

If so, just add some put _all_ statements in your code. E.g.,
[pre]
data balance;
bal=1000;
put _all_;
do i=1 to 12;
put _all_;
bal=bal*1.01;
put _all_;
output;
end;
run;
[/pre]

HTH,
Art
KyleS83
Calcite | Level 5
thanks for the input, it definitely helped me understand it more. Any other explanations would be greatly appreciated as well...Thanks!
Patrick
Opal | Level 21
Hi KyleS83

The documentation to start with for better understanding how SAS works is:
SAS(R) 9.2 Language Reference: Concepts, Second Edition, http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a002586295.htm
You could read through section "Data Step Processing".

If you want to take it a step further then this excellent paper from Paul Dorfman might be a good start: http://support.sas.com/resources/papers/proceedings09/010-2009.pdf

Don't use the techniques Paul Dorfman describes before your SAS skills are on a very solid level.

HTH
Patrick

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 820 views
  • 0 likes
  • 4 in conversation