Jump Statements

During normal execution, each statement in a program is executed in sequence, one after another. The GOTO and LINK statements cause a SAS/IML program to jump from one statement in a program to another statement without executing intervening statements. The place to which execution jumps is identified by a label, which is a name followed by a colon placed before an executable statement. You can program a jump by using either the GOTO statement or the LINK statement:

GOTO label ;

LINK label ;

Both the GOTO and LINK statements instruct SAS/IML software to jump immediately to a labeled statement. However, if you use a LINK statement, then the program returns to the statement following the LINK statement when the program executes a RETURN statement. The GOTO statement does not have this feature. Thus, the LINK statement provides a way of calling sections of code as if they were subroutines. The statements that define the subroutine begin with the label and end with a RETURN statement. LINK statements can be nested within other LINK statements; any number of nesting levels is allowed.

Note: The GOTO and LINK statements must be inside a module or DO group. These statements must be able to resolve the referenced label within the current unit of statements. Although matrix symbols can be shared across modules, statement labels cannot. Therefore, all GOTO statement labels and LINK statement labels must be local to the DO group or module.

The GOTO and LINK statements are not often used because you can usually write more understandable programs by using DO groups and modules. The following statements shows an example of using the GOTO statement, followed by an equivalent set of statements that do not use the GOTO statement:

x = -2;
do;                               
   if x<0 then goto negative;     
   y = sqrt(x);                     
   print y;                       
   goto TheEnd;                          
negative:                         
   print "Sorry, value is negative";  
TheEnd:
end;                              

/* same logic, but without using a GOTO statement */
if x<0 then print "Sorry, value is negative";
else do;
   y = sqrt(x);
   print y;
end;       

The output of each section of the program is identical. It is shown in Figure 6.1.

Figure 6.1: Output That Demonstrates the GOTO Statement

Sorry, value is negative

Sorry, value is negative


The following statements show an example of using the LINK statements. You can also rewrite the statements in a way that avoids using the LINK statement.

x = -2;
do;                                       
   if x<0 then link negative;             
   y = sqrt(x);                             
   print y;                               
   goto TheEnd;                          
negative:                                 
   print "Using absolute value of x";
   x = abs(x);                              
   return;                               
TheEnd:
end;  

The output of the program is shown in Figure 6.2.