FOCUS AREAS

SAS/IML Matrices and Matrix Operators

A matrix is the fundamental structure in the SAS/IML language, and the software provides many ways to create matrices. You associate a name with a matrix when you create or define the matrix. Because a matrix name exists independently of values, you can change the values associated with a particular matrix name, change the dimension of the matrix, or even change its type (numeric or character). You can also assign new values to a matrix at any time.

You can create matrices by using any of the following methods:

Entering Data as a Matrix Literal to Create Matrices

The simplest way to create a matrix is to define a matrix literal by entering the matrix elements. A matrix literal can contain numeric or character data. A matrix literal can be a single element (called a scalar), a single row of data (called a row vector), a single column of data (called a column vector), or a rectangular array of data (called a matrix). The dimension of a matrix is given by its number of rows and number of columns. An n x p has n rows and p columns.

You can specify any of the following types of elements:

Using Assignment Statements to Create Matrices

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.

Using Functions That Generate Matrices

SAS/IML software has many useful built-in functions that generate matrices:

Functions That Generate Matrices

Function Purpose
BLOCK creates a block-diagonal matrix
DESIGNF creates a full-rank design matrix
I creates an identity matrix
J creates a matrix of a given dimension
REPEAT creates a new matrix by repeating elements of the argument matrix
SHAPE shapes a new matrix from the argument

 

You can create matrices as a result of a function call. Scalar functions such as LOG or SQRT operate on each element of a matrix, whereas matrix functions such as INV or RANK operate on the entire matrix.

You can also create a row vector by using the index operator (:). You can use the index operator to count up, count down, or to create a vector of character values with numerical suffixes.

Creating Submatrices from Existing Matrices with Subscripts

Subscripts are special postfix operators placed in square brackets ([ ]) after a matrix operand. You can use subscripts to do any of the following:

You can also use index vectors generated by the index creation operator (:) in subscripts to refer to successive rows or columns. Because all matrices are stored in row-major order, you can index multiple elements of a matrix by listing the position of the elements in an matrix.

Using SAS Data Sets to Create Matrices

SAS/IML software has many statements for passing data from SAS data sets to matrices and from matrices to SAS data sets: you can create matrices from the variables and observations of a SAS data set in several ways. You can create a column vector for each data set variable, or you can create a matrix whose columns correspond to data set variables. You can use all the observations in a data set or use a subset of them.

When you read a SAS data set, you can read any number of observations into a matrix either sequentially, directly by record number, or conditionally according to conditions in a WHERE clause. You can also index a SAS data set. The indexing capability facilitates retrieval by the indexed variable.

Matrix Operators

After you define matrices, you have access to many operators for forming matrix expressions. Operators used in matrix expressions fall into three general categories:

The currently available operators categorized by function, are as follows:

All operators can work on scalars, vectors, or matrices, provided that the operation makes sense. You can write compound expressions that involve several matrix operators and operands.