Control statements the SAS/IML programming language enable you to control the path of execution in a program. These statements are similar to the corresponding statements in the SAS DATA step. There are essentially six different types of control statements in the SAS/IML programming language:
Selection statemements choose one of several control paths in a program. The SAS/IML language supports the IF-THEN and the IF-THEN/ELSE statements. You can use an IF-THEN statement to test an expression and to conditionally perform an operation. You can also optionally specify an ELSE statement. The general form of the IF-THEN/ELSE statement is as follows:
IF expression THEN statement1 ;
ELSE statement2 ;
The expression is evaluated first. If the value of expression is true (which means nonzero and nonmissing), the THEN statement is executed. If the value of expression is false (which means zero or missing), the ELSE statement (if present) is executed. If an ELSE statement is not present, control passes to the next statement in the program.
Several statements can be grouped together into a compound statement (also called a block or a DO group). You use a DO statement to define the beginning of a DO group and an END statement to define the end. DO groups have two principal uses:
DO groups have the following general form:
DO;
statements;
END;
The SAS/IML language provides the following four variations of a DO statement that iterate over compound statements:
DO DATA;
The DATA keyword specifies that iteration stops when an end-of-file condition occurs. You can use the DO DATA statement to read data from an external file or to process observations from a SAS data set.
DO variable = start TO stop <BY increment>;
The value of the variable matrix is initialized to the value of the start matrix. This value is then incremented by the increment value (or by 1 if increment is not specified) until it is greater than or equal to the stop value. (If increment is negative, then the iterations stop when the value is less than or equal to stop.)
DO WHILE expression;
With a WHILE clause, the expression is evaluated at the beginning of each loop, with iterations continuing until the expression is false (that is, until the expression contains a zero or a missing value).
DO UNTIL expression;
The UNTIL clause is like the WHILE clause except that the expression is evaluated at the bottom of the loop. This means that the loop always executes at least once.
Normally, SAS/IML software executes each statement in a program in sequence. However, 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 destination statement 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;
Modules are used to create a user-defined subroutine or function. A module definition begins with a START statement, which has the following general form:
START <name> <( arguments )> <GLOBAL( arguments )>;
A module definition ends with a FINISH statement, which has the following general form:
FINISH <name>;
To execute a module, you can use either a RUN statement or a CALL statement. The general forms of these statements are as follows:
RUN <name> <( arguments)>;
CALL <name> <( arguments)>;
The only difference between the RUN and CALL statements is the order of resolution.
You can stop execution with a PAUSE, STOP, or ABORT statement. The QUIT statement is also a termination statement, but it causes the IML procedure to immediately exit. The other termination statements do not cause PROC IML to exit until the statements are executed.