BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JonDickens1607
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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. 

View solution in original post

6 REPLIES 6
KachiM
Rhodochrosite | Level 12

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

 

jklaverstijn
Rhodochrosite | Level 12

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

 

 

 

Astounding
PROC Star

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. 

JonDickens1607
Obsidian | Level 7

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.

Astounding
PROC Star

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.

JonDickens1607
Obsidian | Level 7
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.

##- Please type your reply above this line. Simple formatting, no
attachments. -##

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1800 views
  • 0 likes
  • 4 in conversation