Question:
When using two or more Do Loops should they always be NESTED or can they OVERLAP?
DO /* Start of Do Loop One */
SAS Statements for Loop One ;
DO /* Start of Do Loop Two */
SAS Statements for Loop Two ;
END ; /* End of Do Loop Two */
More SAS Statements for Loop One ;
END ; /* End of Do Loop One */
/*******************************************************************************/
DO /* Start of Do Loop One */
SAS Statements for Loop One ;
DO /* Start of Do Loop Two */
SAS Statements for Loop Two ;
SAS Statements for Loop One ;
END ; /* End of Do Loop One */
More SAS Statements for Loop Two ;
END ; /* End of Do Loop Two */
If it is legal to overlap Do Loop structures, when would you need to do so?
Regards
The top form of your program is the one that SAS will execute. SAS would never interpret your statements in a way that executes the bottom form.
Here's the rule (assuming you don't have any SELECT groups which also end with an END statement). When SAS encounters an END statement, which DO does it match up with? 100% of the time, it matches the most recent not-yet-ended DO statement.
See the BOLD comments made in the body of your code.
DO /* Start of Do Loop One */
SAS Statements for Loop One ;
DO /* Start of Do Loop Two */
SAS Statements for Loop Two ;
END ; /* End of Do Loop Two */
More SAS Statements for Loop One ;
END ; /* End of Do Loop One */
** The above do-loops are the customary DO-LOOPS. They are efficient in terms computing time. ;
** No repeated computations are done;
/*******************************************************************************/
DO /* Start of Do Loop One */
SAS Statements for Loop One ;
DO /* Start of Do Loop Two */
SAS Statements for Loop Two ;
SAS Statements for Loop One ;
** The above statement is repeated as many times as the Do Loop Two, while unchanging the computed
value as would have done with Loop One. ;
** But, if this statement involves VARIABLES from Do Loop Two, then the computed value will change
for every iteration of the Do Loop Two. In this case, it is necessary and efficient. ;
END ; /* End of Do Loop One */
More SAS Statements for Loop Two ;
END ; /* End of Do Loop Two */
@JonDickens1607 wrote:
Question:
When using two or more Do Loops should they always be NESTED or can they OVERLAP?
That's a clear yes. If only because SAS has no way of knowing you ment to end an outer loop rather than the current innermost DO with your END statement. I think it also raises some logical issues as well.
Regards,
- Jan.
The top form of your program is the one that SAS will execute. SAS would never interpret your statements in a way that executes the bottom form.
Here's the rule (assuming you don't have any SELECT groups which also end with an END statement). When SAS encounters an END statement, which DO does it match up with? 100% of the time, it matches the most recent not-yet-ended DO statement.
Thanks,
That is what I expected but I wanted to be sure.
Does the same approach apply to DATA Steps and SAS Procedures with respect to RUN Statements?
I have seen some SAS code where DATA Steps and SAS Procedures are not executed sequentially
by their own corresponding RUN statements and they are not nested but seem to overlap.
Each DATA and PROC step runs independently of every other step. There is no overlap.
There are a handful of cases that might look slightly different. Some procedures don't end with a RUN statement. There can be several groups of statements (each with their own RUN statement) as part of the same procedure. They are not nested, however. The entire procedure would end with a QUIT statement in that case.
Within a DATA step, CALL EXECUTE can make additional SAS statements part of the program. Any DATA and PROC steps generated in this way do not execute until the current DATA step finishes, however.
Finally, by leaving out a RUN statement, you can get a step to run later, when SAS sees the next DATA or PROC step beginning. But the steps still run sequentially.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.