Tutorial: A Module for Linear Regression |
Because the syntax of the SAS/IML language is similar to the notation used in linear algebra, it is often possible to directly translate mathematical methods from matrix-algebraic expressions into executable SAS/IML statements. For example, consider the problem of solving three simultaneous equations:
These equations can be written in matrix form as
and can be expressed symbolically as
where is the matrix of coefficients for the linear system. Because is nonsingular, the system has a solution given by
This example solves this linear system of equations.
Define the matrices and . Both of these matrices are input as matrix literals; that is, you type the row and column values as discussed in Chapter 3, Understanding the SAS/IML Language.
proc iml; a = {3 -1 2, 2 -2 3, 4 1 -4}; c = {8, 2, 9};
Solve the equation by using the built-in INV function and the matrix multiplication operator. The INV function returns the inverse of a square matrix and * is the operator for matrix multiplication. Consequently, the solution is computed as follows:
x = inv(a) * c;o print x;
Equivalently, you can solve the linear system by using the more efficient SOLVE function, as shown in the following statement:
x = solve(a, c);
After SAS/IML executes the statements, the rows of the vector x contain the , and values that solve the linear system.
You can end PROC IML by using the QUIT statement:
quit;
Copyright © SAS Institute, Inc. All Rights Reserved.