SAS Interactive Matrix Language Computing for Research I Spring 2012 Ramesh
Solve this system of linear equations: 3x + 2y − 4z = 11 5x − 4y = 9 3y + 10z = 42 In matrix form = =
Reading in Data proc iml; n = 3; *scalar; b = { }; *1 x 3 row vector; A = {3 2 -4, , }; *3 x 3 matrix; print n b a; quit;
Matrix Operators: Arithmetic Addition: + Subtraction: - Division, Elementwise: / Multiplication, Elementwise: # Multiplication, Matrix: * Power, Elementwise: ## Power, Matrix: ** Concatenation, Horizontal: || Concatenation, Vertical: // Number of rows: nrow() Number of columns: ncol()
Subscript Operations [ ] Addition + Multiplication # Mean : Sum of squares ## Maximum <> Minimum >< Index of maximum Index of minimum >:< Transpose: ` (Near number 1 on keyboard) Determinant: det(matrix) Inverse: inv(matrix) Trace: tr(matrix) Select a single element: i,j Select a row: i, Select a column:,j
Creating special Matrices: Identity matrix with dimension = size : I(size) Matrix having # rows = nrow # cols = ncol with all elements = x : j(nrow,ncol,x) Diagonal matrix: diag(vector) (diag(matrix)) Block diagonal matrix: block (M1, M2,...)
proc iml; call randseed( ); y = j(400,1); call randgen(y,normal'); z = j(100,1); call randgen(z,'normal', 2, 2.5); x = y // z; create a var{"x"}; append; close a; submit; proc univariate data=a; var x; histogram /kernel; run; endsubmit; Calling SAS PROC’s into IML
proc iml; do i=1 to 3; call randseed( ); mean=i*2; var=0.5*i; y = j(400,1); call randgen(y, 'normal'); z = j(100,1); call randgen(z, 'normal',mean, var); x = y // z; create a var {"x"}; append; close a; submit; proc univariate data=a noprint; var x; histogram / kernel; run; endsubmit; end; Using DO loops
data mult; input v1 v2 v3; datalines; ; proc iml; use mult; read all into v; X=v`*v; print v; run;