Solved
Contributor
Posts: 59

# Nesting and / or Overlapping DO LOOP Structures in SAS

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

Accepted Solutions
Solution
‎07-10-2016 04:57 PM
Super User
Posts: 6,781

## Re: Nesting and / or Overlapping DO LOOP Structures in SAS

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.

All Replies
Super Contributor
Posts: 324

## Re: Nesting and / or Overlapping DO LOOP Structures in SAS

[ Edited ]

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 */

Valued Guide
Posts: 534

## Re: Nesting and / or Overlapping DO LOOP Structures in SAS

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.

Solution
‎07-10-2016 04:57 PM
Super User
Posts: 6,781

## Re: Nesting and / or Overlapping DO LOOP Structures in SAS

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.

Contributor
Posts: 59

## Re: Nesting and / or Overlapping DO LOOP Structures in SAS

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.

Super User
Posts: 6,781

## Re: Nesting and / or Overlapping DO LOOP Structures in SAS

[ Edited ]

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.

Contributor
Posts: 59

## Re: Nesting and / or Overlapping DO LOOP Structures in SAS

yes, I think the last example would probably be close to what I saw and I
wanted to add a run statement but it made it look as if the two code
segments were overlapping.

attachments. -##
☑ This topic is solved.

Discussion stats
• 6 replies
• 526 views
• 0 likes
• 4 in conversation