SAS/IML software has many useful built-in functions that generate matrices. For example, the J function creates a matrix with a given dimension and specified element value. You can use this function to initialize a matrix to a predetermined size. Here are several functions that generate matrices:
The sections that follow illustrate the functions that generate matrices. The output of each example is generated automatically by using the RESET PRINT statement:
reset print;
The BLOCK function has the following general form:
BLOCK
(matrix1,<matrix2,…,matrix15>) ;
The BLOCK function creates a block-diagonal matrix from the argument matrices. For example, the following statements form a block-diagonal matrix:
a = {1 1, 1 1}; b = {2 2, 2 2}; c = block(a,b);
The J function has the following general form:
J
(nrow <,ncol <,value>>) ;
It creates a matrix that has nrow rows, ncol columns, and all elements equal to value. The ncol and value arguments are optional; if they are not specified, default values are used. In many statistical applications, it is helpful to be able to create a row (or column) vector of ones. (You did so to calculate coffee totals in the previous section.) You can do this with the J function. For example, the following statement creates a column vector of ones:
ones = j(5,1,1);
The I function creates an identity matrix of a given size. It has the following general form:
I
(dimension) ;
where dimension gives the number of rows. For example, the following statement creates a identity matrix:
I3 = I(3);
The DESIGNF function generates a full-rank design matrix, which is useful in calculating ANOVA tables. It has the following general form:
DESIGNF
(column-vector) ;
For example, the following statement creates a full-rank design matrix for a one-way ANOVA, where the treatment factor has three levels and there are , , and observations at the factor levels:
d = designf({1,1,1,2,2,3,3});
The REPEAT function creates a new matrix by repeating elements of the argument matrix. It has the following syntax:
REPEAT
(matrix, nrow, ncol) ;
The function repeats matrix a total of nrow ncol times. The argument is repeated nrow times in the vertical direction and ncol times in the horizontal direction. For example, the following statement creates a matrix:
x = {1 2, 3 4}; r = repeat(x, 2, 3);
Figure 5.12: A Matrix of Repeated Values
r 4 rows 6 cols (numeric) |
1 | 2 | 1 | 2 | 1 | 2 |
3 | 4 | 3 | 4 | 3 | 4 |
1 | 2 | 1 | 2 | 1 | 2 |
3 | 4 | 3 | 4 | 3 | 4 |
The SHAPE function creates a new matrix by reshaping an argument matrix. It has the following general form:
SHAPE
(matrix, nrow <,ncol <,pad-value>>) ;
The ncol and pad-value arguments are optional; if they are not specified, default values are used. The following statement uses the SHAPE function to create a matrix that contains the values 99 and 33. The function cycles back and repeats values to fill in the matrix when no pad-value is given.
aa = shape({99 33, 33 99}, 3, 3);
Alternatively, you can specify a value for pad-value that is used for filling in the matrix:
bb = shape({99 33, 33 99}, 3, 3, 0);
The SHAPE function cycles through the argument matrix elements in row-major order and fills in the matrix with zeros after the first cycle through the argument matrix.