Understanding the SAS/IML Language


Functions

The general form of a function is result = FUNCTION(arguments) where arguments is a list of matrix names, matrix literals, or expressions. Functions always return a single matrix, whereas subroutines can return multiple matrices or no matrices at all. If a function returns a character matrix, the matrix to hold the result is allocated with a string length equal to the longest element, and all shorter elements are padded on the right with blanks.

Categories of Functions

Many functions fall into one of the following general categories:

scalar functions operate on each element of the matrix argument. For example, the ABS function returns a matrix with elements that are the absolute values of the corresponding elements of the argument matrix.

matrix inquiry functions return information about a matrix. For example, the ANY function returns a value of 1 if any of the elements of the argument matrix are nonzero.

summary functions return summary statistics based on all elements of the matrix argument. For example, the SSQ function returns the sum of squares of all elements of the argument matrix.

matrix reshaping functions manipulate the matrix argument and returns a reshaped matrix. For example, the DIAG function returns a diagonal matrix with values and dimensions that are determined by the argument matrix.

linear algebraic functions perform matrix algebraic operations on the argument. For example, the TRACE function returns the trace of the argument matrix.

statistical functions perform statistical operations on the matrix argument. For example, the RANK function returns a matrix that contains the ranks of the argument matrix.

The SAS/IML language also provides functions in the following general categories:

  • matrix sorting and BY-group processing

  • numerical linear algebra

  • optimization

  • random number generation

  • time series analysis

  • wavelet analysis

See the section Statements, Functions, and Subroutines by Category for a complete listing of SAS/IML functions.

Exceptions to the SAS DATA Step

The SAS/IML language supports most functions that are supported in the SAS DATA step. These functions almost always accept matrix arguments and usually act elementwise so that the result has the same dimension as the argument. See the section Base SAS Functions Accessible from SAS/IML Software for a list of these functions and also a small list of functions that are not supported by SAS/IML software or that behave differently than their Base SAS counterparts.

The SAS/IML random number functions UNIFORM and NORMAL are built-in functions that produce the same streams as the RANUNI and RANNOR functions, respectively, of the DATA step. For example, you can use the following statement to create a $10 \times 1$ vector of random numbers:

x = uniform(repeat(0,10,1));

SAS/IML software does not support the OF clause of the SAS DATA step. For example, the following statement cannot be interpreted in SAS/IML software:

a = mean(of x1-x10); /* invalid in the SAS/IML language */

The term x1-x10 would be interpreted as subtraction of the two matrix arguments rather than its DATA step meaning, the variables X1 through X10.