Haas MFE SAS Workshop Lecture 3: Peng Liu http://faculty.haas.berkeley.edu/peliu/computing Haas School of Business, Berkeley, MFE 2006
Peng Liu http://faculty.haas.berkeley.edu/peliu/computing SAS IML Peng Liu http://faculty.haas.berkeley.edu/peliu/computing Haas School of Business, Berkeley, MFE 2006
PROC IML - What OUTLINES Defining Matrix Creating Matrix Combining Matrix Matrix Algebra (Function, Reduction Operators) Read-In SAS data sets Create SAS data sets Haas School of Business, Berkeley, MFE 2006 Peng Liu AND Alexander Vedrashko 3
Defining and Referencing matrices PROC IML; RESET PRINT; A=3; B={1 2 3}; *row vector; C={1,2,3}; *column vector; D={1 2 3, 4 5 6};*2 X 3 matrix; QUIT; PROC IML; D={1 2 3, 4 5 6}; E1=D[,2]; E2=D[1,]; E3=D[2,3]; E4=D[{1 3},{2 3}]; PRINT D E1 E2 E3 E4; QUIT; Begin with PROC IML; end with QUIT; RESET PRINT produce all output; otherwise specify what you want to PRINT , but you have to assign first. Use curly brackets { } to create vector or matrix Use square bracket [] to refer to elements E4=D[{1 3},{2 3}]; assigns submatrix to E4 consisting 1,3 rows and 2,3 columns of matrix D Haas School of Business, Berkeley, MFE 2006 Peng Liu AND Alexander Vedrashko 4
Creating matrices PROC IML; RESET PRINT; index=1:5; rindex=5:1; vars='X1':'X8'; series = do(0,84,12); aa = I(6); bb = j(5,5,0); cc = j(6,1); dd = diag({1 2 4}); ee = diag({1 2,3 4}); QUIT; Index operator : creates a row vector of consecutive integers or character strings Do function creates series with any increment Identity matrix: I(size) Constant matrix J(nrow, ncol, value) Diagonal matrices: DIAG(VECTOR) Haas School of Business, Berkeley, MFE 2006 Peng Liu AND Alexander Vedrashko 5
Combining matrices Transpose operation using t() PROC IML; RESET PRINT; b={1 2 3 4}; bt=t(b); c={1 2 3 4,2 4 6 8, 3 9 7 5 ,9 8 7 6}; i1=c//b;i2=c||bt; QUIT; Transpose operation using t() "//" and ||" concatenates matrices vertically and horizontally respectively Haas School of Business, Berkeley, MFE 2006 Peng Liu AND Alexander Vedrashko 6
Matrix Algebra - Basics Haas School of Business, Berkeley, MFE 2006 Peng Liu AND Alexander Vedrashko 7
Matrix Algebra – Matrix functions Haas School of Business, Berkeley, MFE 2006 Peng Liu AND Alexander Vedrashko 8
Matrix Algebra – Matrix Reduction Operators Haas School of Business, Berkeley, MFE 2006 Peng Liu AND Alexander Vedrashko 9
Read SAS data sets PROC IML; RESET PRINT; USE MFE.LOAN; READ ALL INTO m WHERE(state='CA'); QUIT; *READ ALL VAR{id term rate} INTO m; USE …; specify which SAS data set is to be used READ ALL INTO …; converts SAS data set into a matrix. READ ALL only read numerical variables; Use VAR{term rate} option to select variables Use WHERE to subset data. Haas School of Business, Berkeley, MFE 2006 Peng Liu AND Alexander Vedrashko 10
Create SAS data sets PROC IML; RESET PRINT; seed=J(3,3,9); r = ranuni(seed); CREATE data FROM r; APPEND FROM r; QUIT; J function build 3 by 3 matrix with seed=9 for each element CREATE … FROM opens the new SAS data set APPEND FROM writes to the SAS data set Output dataset has default column names COL1 COL2… You can rename by option [COLNAME = “R”] Haas School of Business, Berkeley, MFE 2006 Peng Liu AND Alexander Vedrashko 11