Programming Statements |
During normal execution, statements are executed one after another. The GOTO and LINK statements instruct IML to jump from one part of a program to another. 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:
Both the GOTO and LINK statements instruct IML to jump immediately to the labeled statement. The LINK statement, however, reminds IML where it jumped from so that execution can be returned there if a RETURN statement is encountered. 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 LINK statement calls the routine. The routine begins with the label and ends with a RETURN statement. LINK statements can be nested within other LINK statements; any number of nesting levels is allowed.
CAUTION: The GOTO and LINK statements are limited to being 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 other features, such as DO groups for conditionals, iterative DO groups for looping, and module invocations for subroutine calls.
Here are two DO groups that illustrate how the GOTO and LINK statements work:
do; do; if x<0 then goto negative; if x<0 then link negative; y=sqrt(x); y=sqrt(x); print y; print y; stop; stop; negative: negative: print "Sorry, X is negative"; print "Using abs. value of negative X"; end; x=abs(x); return; end;
The following is a comparable way to write the program on the left without using GOTO or LINK statements:
if x<0 then print "Sorry, X is negative"; else do; y=sqrt(x); print y; end;
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.