Multivariate Statistical Techniques
Matrix Operations in SAS IML


The Matrix

proc iml;

A = {2 1,3 2,-2 2};

print A;

       A
       2         1
       3         2
      -2         2

Multiplication by a Scalar

c = 3;

D = c*a;

print c D;

        C         D
        3         6         3
                  9         6
                 -6         6

Matrix Addition & Subtraction

B = {1 1,4 2,-2 1};
print B;

        B
        1         1
        4         2
       -2         1

C = A + B;

print C;

        C
        3         2
        7         4
       -4         3

D = A - B;

print D;

        D
        1         0
       -1         0
        0         1

Matrix Multiplication

D = {2 1 3,-2 2 1};

C = D*A;

Print D C;

        D                             C
        2         1         3         1        10
       -2         2         1         0         4

C = A*D;

print C;

        C
        2         4         7
        2         7        11
       -8         2        -4

D = {2 1 3};

C = D*A;

print C D A;

        C                   D                             A
        1        10         2         1         3         2         1
                                                          3         2 
                                                         -2         2

C = A*D;

ERROR: (execution) Matrices do not conform to the operation.

Transpose of a Matrix

AT = A`;

ATT = AT`;

print AT ATT;

       AT                           ATT
        2         3        -2         2         1
        1         2         2         3         2
                                     -2         2

Common Vectors

Unit Vector

U = J(3,1,1);

print U;

        U
        1
        1
        1

Common Matrices

Unit Matrix

U = J(3,2,1);

print U;

        U
        1         1
        1         1
        1         1

Diagonal Matrix

S = {2 1 4,3 2 2,-2 2 3};

D = diag(S);

print D S;

        D                             S
        2         0         0         2         1         4
        0         2         0         3         2         2
        0         0         3        -2         2         3

Identity Matrix

I = I(3);

print I;

        I
        1         0         0
        0         1         0
        0         0         1

Inverse of a Matrix

A = {4 2 2,4 6 8,-2 2 4};

B = inv(A);

C = B*A;

D = A*B;

print A B C D;

        A                             
        4         2         2        
        4         6         8       
       -2         2         4       

        B
        1      -0.5       0.5
       -4       2.5       -3 
       2.5     -1.5        2

        C
        1         4.441E-16  4.441E-16
      -2.66E-15    1          0
        2          0        -8.88E-16

        D
        1          0         0
      -2.66E-15    1         0
      -1.33E-15  -4.44E-16         1

Determinant of a Matrix

d = det(A);

print d;

        D
        8

Computing Column & Row Sums

X = {3 2,2 -2,4 6,3 1};

c = X(|+,|);

r = X(|,+|);

t = X(|+,+|);

print X c r t;

        X                   C                   R         T
        3         2        12         7         5        19
        2        -2                             0
        4         6                            10
        3         1                             4

Computing Column & Row Means

c = X(|:,|);

r = X(|,:|);

g = X(|:,:|);

       C                   R           T
       3         1.75     2.5         2.375
                           0
                           5
                           2

Horizontal Concatenation

A = {2 1,3 2,-2 2};

B = {1 1,3 4,2 2};

C =  A||B;

print C;

        C
        2         1         1         1
        3         2         3         4
       -2         2         2         2

Vertical Concatenation (Appending)

C =  A//B;

print C;

        C
        2         1
        3         2
       -2         2
        1         1
        3         4
        2         2


Multivariate Course Page

Phil Ender, 30jun98