BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
faithmuturi94
Calcite | Level 5

do loop question.png

 

This is a question from a practice exam I just took and I am not understanding how this iterative do loop works. Can anyone please explain why the increment of i goes past the specified range and why there is a iterative do loop within the original do loop? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Iterated do loops ALWAYS go until the condition is no longer true. Since there is no increment included I increments by one until the value is greater than the limit.

 

Hint: With short examples like this type the code into your SAS editor and run it.

Maybe place some put statements in various places to see what changes where/when.

 

Consider this short program. Copy and paste into your editor and run it then read the LOG.

 

data example;
   do i= 0 to 5;
     put "in loop " i=;
   end;
   put 'after loop ' i=;
run;

You can look in the log to see what happens at each iteration and examine the data set to see what the value is when the data set is created.

 

Then look at this one:

data example2;
   do i= 0 to 15 by 5;
     put "in loop " i=;
   end;
   put 'after loop ' i=;
run;

"why there is a iterative do loop within the original do loop?" Better to say OUTER than original. Looks like someone is simulating a 10% increase in a value every calendar quarter within each year. Maybe a crude compound interest accumulation.

 

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Iterated do loops ALWAYS go until the condition is no longer true. Since there is no increment included I increments by one until the value is greater than the limit.

 

Hint: With short examples like this type the code into your SAS editor and run it.

Maybe place some put statements in various places to see what changes where/when.

 

Consider this short program. Copy and paste into your editor and run it then read the LOG.

 

data example;
   do i= 0 to 5;
     put "in loop " i=;
   end;
   put 'after loop ' i=;
run;

You can look in the log to see what happens at each iteration and examine the data set to see what the value is when the data set is created.

 

Then look at this one:

data example2;
   do i= 0 to 15 by 5;
     put "in loop " i=;
   end;
   put 'after loop ' i=;
run;

"why there is a iterative do loop within the original do loop?" Better to say OUTER than original. Looks like someone is simulating a 10% increase in a value every calendar quarter within each year. Maybe a crude compound interest accumulation.

 

 

faithmuturi94
Calcite | Level 5

Excuse the lateness, but thanks so much for the response! This really helped me pass my SAS Base Certification test which had a few do loop questions. Cheers! 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 2 replies
  • 370 views
  • 1 like
  • 2 in conversation