Language Reference

LEAVE Statement

  • LEAVE ;

This statement is supported by the IML procedure and the iml action.

The LEAVE statement stops the processing of the current DO loop and continues processing at the next statement following the DO loop. The LEAVE statement can be used only inside a DO loop.

You can use the LEAVE statement to exit a DO loop when a specified condition occurs. For example, the following program exits the DO loop if a missing value is encountered.

x = 1:10;
x[5] = .;

sum = 0;
do i = 1 to ncol(x);
   if x[i] = . then leave;  /* exit the DO loop */
   sum = sum + x[i];
end;
print i sum;

Figure 222: Sum of Values Until First Missing Value

rcx
00.0307522
 0.0619692
 0.0929721
 0.1415983


Notice that the value of the iteration counter (i) indicates the iteration at which the LEAVE statement was executed.

The LEAVE statement can be used to exit a DO loop in the middle of the loop. To exit a DO loop at the top of the loop, use a DO-WHILE statement. To exit a DO loop at the bottom of the loop, use a DO-UNTIL statement. To skip the current iteration but proceed to the next iteration, use a CONTINUE statement.

Last updated: April 16, 2021