Primer on Using PROC IML SS Interactive Matrix Language (IML) is a SS procedure. IML is a matrix language and has built-in operators and functions for most standard matrix operations. To invoke the procedure, start with the PROC IML statement and end with. Reading in Data matrix can be defined in two ways in IML: () manually enter them using code, and (2) define a file containing the matrix which is later invoked in IML. Entering Data in a Matrix: *invokes IML; a = 2; *scalar; b = { 2 3 4}; * x 4 row vector; c = {.0 0 0, 0.2.0 0, 0.8 0.4.0}; *3 x 3 (correlation) matrix; *closes IML; The braces {} encloses the values into a matrix and commas are used to separate rows. Providing a file of elements: ssume that you have a space delimited file containing elements of a 9 x 9 (correlation) matrix called holzinger.txt. The following code is used to read in the file and determine a matrix with the elements from the file matrix.txt. filename matrix 'C:\... '; *directory where file is located; data example; *creating a dataset called 'example'; infile matrix(holzinger.txt); *linking the file holzinger.txt ; *to the filename matrix; input x x2 x3 x4 x5 x6 x7 x8 x9; *arbitrary column labels; run; use example; read all var {x x2 x3 x4 x5 x6 x7 x8 x9} into X; *use all 9 columns of data for matrix X; Printing out matrices of interest IML can either be used interactively where statements are executed immediately or noninteractively. For interactive use, it is convenient to use the reset log print; statement which causes IML to display results in the log file, together with input statements. The print option causes IML to display the result of each assignment statement in the output. Jolynn Pek, ll rights 2008
For example, we want to specify a correlation matrix have SS print out the elements of..0 0.5.0 0.6 0.7.0 and wanted to 0.2 0. 0.8.0 Using reset log print; *invokes IML; reset log print; *displays results in the log file; = {.0 0 0 0, 0.5.0 0 0, 0.6 0.7.0 0, 0.2 0. 0.8.0}; *manually entering the matrix; *closes IML; The log file will display this: NOTE: IML Ready reset log print; = {.0 0 0 0, 0.5.0 0 0, 0.6 0.7.0 0, 0.2 0. 0.8.0}; Using print; *invokes IML; = {.0 0 0 0, 0.5.0 0 0, 0.6 0.7.0 0, 0.2 0. 0.8.0}; *manually entering the matrix; print ; *displays matrix in the output; *closes IML; The output will display this: 0 0 0 0.5 0 0 0.6 0.7 0 0.2 0. 0.8 Note that since IML can be used interactively, matrix was printed in the output without executing the statement. Jolynn Pek, ll rights 2008 2
Functions which create matrices a. I(size) Identity matrix where size = number of diagonal s. b. j(nrow,ncol,x) Constant matrix where nrow = number of rows, ncol = number of columns and x = element. c. diag(vector) Creates a square matrix with the elements of the vector along the main diagonal. d. diag(matrix) Creates a square matrix which retains the main diagonal of the argument matrix. Example code: a = I(6); * 6x6 identity matrix; b = j(5,5,0); *5x5 matrix of 0's; c = j(6,); *6x column vector of 's; d=diag({ }); e=diag({ 2, 3 4}); print a; print b; print c; print d; print e; Note, in using the diag() function, the vector or matrix has to be specified in braces {} and not in brackets (). Output: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 B C D 0 0 0 2 0 0 0 4 Matrix Operations E 0 0 4 Jolynn Pek, ll rights 2008 3
Matrix Operations a. ddition operator: + b. Subtraction operator: c. Sign reverse operator: d. bsolute function: abs(matrix) e. Matrix product operator: * f. Square root function: sqrt(matrix) g. Transpose operator: t(matrix) or ` (backquote character) h. Determinant: det(matrix) i. Matrix inverse operator: inv(matrix) j. Trace of a matrix: tr(matrix) k. Eigen values: eigval(matrix) l. Eigen vectors: eigvec(matrix) m. Minimum value: min(matrix) n. Maximum value: max(matrix) Example code: X ={ 2, 3 4}; Y ={-4 3,-2 -}; a = X+X; *addition; b = X-X; *subtraction; c = -Y; *sign reversal; d = abs(y); *takiing absolute value; e = X*X; *matrix multiplication; f = sqrt(x); *taking square roots of the elements; g = X`; *transpose using back quote; g2 = t(x); *transpose using function; h = det(x); *matrix determinant; i = inv(x); *matrix inverse; j = tr(x); *trace of a matrix; k = eigval(x); *eigen values of matrix; l = eigvec(x); *eigen vector of matrix; m = min(x); *smallest matrix element; n = max(x); *largest matrix element; print a; print b; print c; print d; print e; print f; print g; print g2; print h; print i; print j; print k; print l; print m; print n; Jolynn Pek, ll rights 2008 4
Output: 6 8 G 3 B 0 0 0 0 G2 3 C H 4-3 2-2 D 4 3 2 E 7 0 5 22 F.44236.7320508 2 I -2.5-0.5 K 5.372283 0-0.37228 0 L 0.459736-0.824565 0.9093767 0.5657675 M N 4 Jolynn Pek, ll rights 2008 5