GOTO Statement

Directs program execution immediately to the statement label that is specified and, if followed by a RETURN statement, returns execution to the beginning of the DATA step.

Valid in: DATA step
Categories: CAS
Control
Type: Executable
Alias: GOTO

Syntax

GOTO label;

Arguments

label

specifies a statement label that identifies the GOTO destination. The destination must be within the same DATA step. You must specify the label argument.

Comparisons

The GOTO statement and the LINK statement are similar. However, a GOTO statement is often used without a RETURN statement, whereas a LINK statement is usually used with an explicit RETURN statement. The action of a subsequent RETURN statement differs between the GOTO and LINK statements. A RETURN statement after a LINK statement returns execution to the statement that follows the LINK statement. A RETURN after a GOTO statement returns execution to the beginning of the DATA step (unless a LINK statement precedes the GOTO statement. In that case, execution continues with the first statement after the LINK statement).

GOTO statements can often be replaced by DO-END and IF-THEN/ELSE programming logic.

Example: Using a RETURN Statement with the GOTO Statement

Use the GOTO statement as shown here.

  • In this example, if the condition is true, the GOTO statement instructs SAS to jump to a label called ADD and to continue execution from there. If the condition is false, SAS executes the PUT statement and the statement that is associated with the GOTO label.
    data info;   
       input x;
       if 1<=x<=5 then goto add;
       put x=;
       add: sumx+x;
       datalines;
    7
    6
    323
    ;

    Because every DATA step contains an implied RETURN at the end of the step, program execution returns to the top of the step after the sum statement is executed. Therefore, an explicit RETURN statement at the bottom of the DATA step is not necessary.

  • If you do not want the Sum statement to execute for observations that do not meet the condition, rewrite the code and include an explicit return statement.
    data info;
       input x;
       if 1<=x<=5 then goto add;
       put x=;
       return;    
          /* SUM statement not executed */
          /* if x<1 or x>5              */
       add: sumx+x;
       datalines;
    7
    6
    323
    ;

See Also

Last updated: June 17, 2025