turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Learning SAS - would like some help understanding ...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-29-2011 05:24 PM

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!

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!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-29-2011 05:49 PM

> 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.

> 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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-29-2011 06:40 PM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-29-2011 06:59 PM

thanks for the input, it definitely helped me understand it more. Any other explanations would be greatly appreciated as well...Thanks!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-29-2011 11:47 PM

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

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