| Working with Matrices |
SAS/IML software has many built-in functions that generate useful matrices. For example, the J function creates a matrix with a given dimension and element value when you supply the number of rows and columns, and an element value for the new matrix. This function is useful to initialize a matrix to a predetermined size. Here are several matrix-generating functions:
The sections that follow illustrate these matrix-generating functions. Again, they are shown with automatic printing of results, activated by invoking the RESET statement with the PRINT option.
reset print;
> a={1 1,1 1};
A 2 rows 2 cols (numeric)
1 1
1 1
> b={2 2, 2 2};
B 2 rows 2 cols (numeric)
2 2
2 2
> c=block(a,b);
Here is the resulting matrix:
C 4 rows 4 cols (numeric)
1 1 0 0
1 1 0 0
0 0 2 2
0 0 2 2
> one=j(1,5,1);
ONE 1 row 5 cols (numeric)
1 1 1 1 1
> I3=I(3);
I3 3 rows 3 cols (numeric)
1 0 0
0 1 0
0 0 1
> d=designf({1,1,1,2,2,3,3});
D 7 rows 2 cols (numeric)
1 0
1 0
1 0
0 1
0 1
-1 -1
-1 -1
Although the nrow, ncol, and pad-value arguments
are optional, you should usually specify them.
The following statement uses the SHAPE function to create a
matrix containing 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,99 33},3,3);
AA 3 rows 3 cols (numeric)
99 33 99
33 99 33
99 33 99
In the next example, a pad-value
is specified for filling in the matrix:
> aa=shape({99 33,99 33},3,3,0);
AA 3 rows 3 cols (numeric)
99 33 99
33 0 0
0 0 0
The SHAPE function cycles through the argument matrix
elements in row-major order and then fills in the matrix with
0s after the first cycle through the argument matrix.
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.