Previous Page | Next Page

Macro Statements

%GOTO Statement

Branches macro processing to the specified label.
Type: Macro statement
Alias: %GO TO
Restriction: Allowed in macro definitions only
See also: %label Statement

Providing Exits in a Large Macro


%GOTO label;


is either the name of the label that you want execution to branch to or a text expression that generates the label. A text expression that generates a label in a %GOTO statement is called a computed %GOTO destination.(footnote 1)

The following examples illustrate how to use label:

  •  %goto findit;  /* branch to the label FINDIT */
  • %goto &home;   /* branch to the label that is */
                   /* the value of the macro variable HOME */
No percent sign (%) precedes the label name in the %GOTO statement.

The syntax of the %GOTO statement does not include a % in front of the label name. If you use a %, the macro processor attempts to call a macro by that name to generate the label.  [cautionend]


Branching with the %GOTO statement has two restrictions. First, the label that is the target of the %GOTO statement must exist in the current macro; you cannot branch to a label in another macro with a %GOTO statement. Second, a %GOTO statement cannot cause execution to branch to a point inside an iterative %DO, %DO %UNTIL, or %DO %WHILE loop that is not currently executing.


Example 1: Providing Exits in a Large Macro

The %GOTO statement is useful in large macros when you want to provide an exit if an error occurs.

%macro check(parm);
   %local status;
   %if &parm= %then %do;
       %put ERROR:  You must supply a parameter to macro CHECK.;
       %goto exit;

   more macro statements that test for error conditions 

   %if &status > 0 %then %do;
       %put ERROR:  File is empty.;
       %goto exit;

   more macro statements that generate text 

   %put Check completed successfully.;
%exit: %mend check;

FOOTNOTE 1:   A computed %GOTO contains % or & and resolves to a label. [arrow]

Previous Page | Next Page | Top of Page