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
- using assignment statements
- using functions that generate matrices
- creating submatrices from existing matrices with subscripts
- using SAS data sets

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:

- a number
- a period (.), which represents a missing numeric value
- a number in brackets ([ ]), which represents a repetition factor
- a character string

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.

SAS/IML software has many useful built-in 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.

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

- refer to a single element of a matrix
- refer to an entire row or column of a matrix
- refer to any submatrix contained within a matrix
- perform a reduction across rows or columns of a matrix. A reduction is a statistical operation (often a sum or mean) applied to the rows or to the columns of a matrix.

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.

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.

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

- 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`).

- addition operator: +
- comparison operators: <, <=, >, >=, =, ^=
- concatenation operator, horizontal: ||
- concatenation operator, vertical: //
- direct product operator: @
- division operator: /
- element maximum operator: <>
- element minimum operator: ><
- index creation operator: :
- logical operators: &, |, ^
- multiplication operator, elementwise: #
- multiplication operator, matrix: *
- power operator, elementwise: ##
- power operator, matrix: **
- sign reversal operator: –
- subscripts: [ ]
- subtraction operator: –
- transpose operator: `

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.