Understanding the SAS/IML Language


Assignment Statements

Assignment statements create matrices by evaluating expressions and assigning the results. The expressions can be composed of operators (for example, matrix multiplication) or functions that operate on matrices (for example, matrix inversion). The resulting matrices automatically acquire appropriate characteristics and values. Assignment statements have the general form result = expression where result is the name of the new matrix and expression is an expression that is evaluated.

Functions as Expressions

You can create matrices as a result of a function call. Scalar functions such as the LOG function or the SQRT function operate on each element of a matrix. Matrix functions such as the INV function or the RANK function operate on the entire matrix. The following statements are examples of function calls:

a = sqrt(b);                         /* elementwise square root */
y = inv(x);                          /* matrix inversion */
r = rank(x);                         /* ranks (order) of elements */

The SQRT function assigns each element of a the square root of the corresponding element of b. The INV function computes the inverse matrix of x and assigns the results to y. The RANK function creates a matrix r with elements that are the ranks of the corresponding elements of x.

Operators within Expressions

Three types of operators can be used in assignment statement expressions. The matrices on which an operator acts must have types and dimensions that are conformable to the operation. For example, matrix multiplication requires that the number of columns of the left-hand matrix be equal to the number of rows of the right-hand matrix.

The three types of operators are as follows:

  • Prefix operators are placed in front of an operand (-A).

  • Binary operators are placed between operands (A*B).

  • Postfix operators are placed after an operand (A$^{\prime }$).

All operators can work on scalars, vectors, or matrices, provided that the operation makes sense. For example, you can add a scalar to a matrix or divide a matrix by a scalar. The following statement is an example of using operators in an assignment statement:

y = x#(x>0);

This assignment statement creates a matrix y in which each negative element of the matrix x is replaced with zero. The statement actually contains two expressions that are evaluated. The expression x>0 is an operation that compares each element of x to zero and creates a temporary matrix of results; an element of the temporary matrix is 1 when the corresponding element of x is positive, and 0 otherwise. The original matrix x is then multiplied elementwise by the temporary matrix, resulting in the matrix y.

See Chapter 25: Language Reference, for a complete listing and explanation of operators.