#### SAS/IML BASICS

Basic Rules

- SAS/IML operates on two-dimensional matrices of number or characters.
- SAS/IML is a program language with a variety of operators, function, and control statements.
- SAS/IML is interactive, although batch job is allowed.
- A matrix cannot have both numeric and character values; only numeric or character matrix is possible.
- Character values may be enclosed in single or double quotation marks. Otherwise, character values are converted to uppercase.
- A brace {} assigns elements of a matrix, while a bracket [] (or ||) is used to indicate the position of elements of a matrix. SAS/IML
- A matrix can contain missing values represented as ".". Any operation with at least one missing value produces missing value. However, multiplication or inverse of a matrix with missing values is not defined.
- SAS/IML is not casesensitive.
- The PRINT command does not allow any function.

Printing

- PRINT c[format=10.4];
- PRINT covar[ROWNAME=r_matrix COLNAME=c_matrix];
- PRINT coffee[r=names c=days];
- PRINT coffee[R=names C=days] weektot[format=DOLLAR7.2], daytot[c=days format=DOLLAR8.2] ' ' total[f=DOLLAR7.2];

#### PROC IML

- SAS/IML begins with "
`PROC IML;`

" and ends with "`QUIT`

;" - In the PROGRAM window, type in "PROC IML;" and summit it. SAS responses with "IML Ready" in the LOG window.
- The RESET command is used to set options for SAS/IML.
- "
`RESET LOG PRINT;`

" produces the output in the LOG window. Default is the OUTPUT window. - AUTONAME (NOAUTONAME) prints row and column labels for matrices.
- NOCENTER (CENTER) aligns the output to the left.
- FUZZ=0 prints small numbers a zero.
- LINESIZE=l PAGESIZE=p set the line size and page size.
- SPACES=s set the number of spaces printed between two adjacent matrices.

#### MATRIX DEFINITION

General Definitions

- x=625; x={625}; /* A numeric scalar */
- name="kucc625"; name='kucc625'; /* A character scalar */
- x={1 2 3 4 5 6 7}; /* row vector */
- x={1, 2, 3, 4, 5, 6, 7}; /* column vector */
- x={1 2 3, 4 5 6}; /* 2 X 3 numeric matrix */
- name={'Park' 'Kim', "Lee" "Choi" }; /* 2 X 2 character matrix */
- name={Park Kim, 'Lee' 'Choi', "Yoon" "Yeom"}; /* "name=Park" is not allowed. */
- boolean={[2] 'True', [2] 'False'}; /* Repeatition Factors to get 2 by 2 */
- boolean={'True' 'True', "False" "False"};

Definition Using Index Vectors

- x=1:5; x={1 2 3 4 5}; x=J(1:5);/* 1 X 5 matrix*/
- x=5:1; x={5 4 3 2 1};
- x=3:5; x={3 4 5); x=J(3:5); /* 1 X 3 matrix */
- x=3:5'; x={3, 4, 5}; /* 3 X 1 matrix */
- name={'Park1': 'Park3'); name={'Park1' 'Park2' 'Park3'};
- x=DO(0,12,4); x={0 4 8 12};

Definition Using Functions

- identity=I(5); /* 5 X 5 identity matrix */
- a=J(3, 2, 7); a={7 7, 7 7, 7 7}; /* 3 X 2 matrix with each element of 7 */
- b=J(5, 1); b={1, 1, 1, 1, 1}; /* 5 X 1 column vector with each element of 1 */
- c=BLOCK(a, b); /* 8 X 3 matrix; a and be are used as partition matrices*/
- d=SHAPE({1 2 3}, 4, 4); /* Repeat the {1 2 3} until filling 4 X 4 matrix */
- d2=SHAPE({1 2 3}, 4, 4, 7); /* Repeat once, and assign 7 the rest elements */
- f=DIAG({1 2 3}); f={1 0 0, 0 2 0, 0 0 3}; /* 3 X 3 diagonal matrix */
- g=DIAG({1 2, 3 4}); g={1 0, 0 4};
- h=VECDIAG({1 2, 3 4}); h={1, 4}; /* column vector from a diagonal matrix */

#### OPERATORS AND FUNCTIONS

Operators

- Arithmetic: +, -, *, /
- Relational: <, >, <=, >=, =, ^=
- Logical: &, |, ^
- Matrices: #, ##, ||, //, @, <>, <=
- a[2, 1]=0; a={1 2, 0 4};
- a[1, ]=7; a={7 2, 7 4}; /* identical to a[1,1:2]=7; */
- a[, 2]=3; a={7 3, 0 3}; /* identical to a[1:2,2]=3; */
- i={1 2}; d=a[i, i]; /* to define d matrix by using a and I matrix */
- sum=a + b; /* a={1 2, 3 4};) */
- substraction = a - b;
- product=a * b; /* a*a={7 10, 15 22}*/
- product=a # b; /*elementwise product a and b, that is a#a={1 4, 9 16}*/
- em=a#b; /* Elementwise multiplication */
- ep=a##b; /* Elementwise power */
- m=MOD(a, 3); /* modulo (remainder) */
- k=a@b; /* Kronecker product */
- c=a||a /*horizental merge */
- c=a//a /*vertical merge */

Subscript Reduction Operators

- +, #, <>, ><, <:>, >:<, :, ##
- a={1 2, 3 4};
- b=a[+,]; /* b, a row vector of {4 6}, is the column sums of a */
- b=a[,+]; /* b, a column vector of {3, 7}, is the row sums of a */
- b=a[##,]; /* b, {10 20}, is the column sums of squares of a */
- b=a[,##]; /* b, {5, 25}, is the column sums of squares of a */
- b=a[,:]; /* b, {2 3}is the mean of each row */
- b=a[:,]; /* b, {1.5, 3.5}is the mean of each column */
- b=a[+,+]; c=sum(a); /* to produce 10, sum of total elements */
- s=a[,+]; s=a[+,] /* sum of rows and columns */
- s=a[{2 3}, +]; /* 2 X 1; sum of 2nd and 2rd rows of matrix a */
- m=a[+, <>]; /* scalar; choose maximum column sum */
- m=a[><, #]; /* scalar; choose minimum row product */
- m=a[>:<,]; /* index of minimums of rows */
- m=a[:]; /*compute mean of all elements */
- s=a[##]; /* sum of squares */
- l=a<>b; /* choosing larger elements*/
- b=a<=b; /* set 1 if the condition meets, 0 otherwise */

Functions

- trans=T({1 2, 3 4}); trans'; trans={1 3, 2 4}; /* transposing a matrix */
- inverse=INV(a); /* to calculate the inverse matrix */
- trace=TRACE(a); /* to calculate the trace of a matrix */
- determin=DET(a); /* to calculate the determinant of a matrix */
- columns=NCOL(a); /* the number of columns */
- rows=NROW(a); /* the number of rows */
- rank=RANK(a); /* matrix rank */
- minimun=MIN(variable1, variable2); /* to find minimal value */
- s=SUM(vector); /* to calculate the sum of all elements */
- ssq=SSQ(vector); /* sum of square */
- sq=SQRT(constant); /* square root */
- abs=ABS(constant); /* absolute value */
- p=2*(1-PROBT(t, df, 0)); /* to get the value of the accumulated probability */

#### INPUT AND OUTPUT

Referring to a Data Set

- LIBNAME js 'k:\'; PROC IML; RESET DEFLIB=js; USE jeeshim;
- Do not specify the library name in front of the data set name; That is, "USE js.jeeshim;'does not work.

Selecting Observations (USE and READ)

- USE jeeshim; READ ALL; /* Read all variables into vectors */
- USE jeeshim; READ ALL INTO x; /* all observations into a matrix x */
- USE jeeshim VAR{country gnp interest population}; READ ALL;
- USE jeeshim VAR{country gnp interest population}; READ ALL INTO y;
- USE jeeshim; READ ALL VAR _NUM_; /* read all numeric variables */
- USE jeeshim; READ ALL VAR _CHAR_; /* all character variables */
- USE jeeshim; READ ALL VAR _NUM INTO n;
- USE jeeshim; READ ALL WHERE gnp >= 1,000; /* WHERE clause */
- USE jeeshim; READ ALL VAR{gnp interest} INTO z; /* VAR clause */

Editing Observations (EDIT, FIND, and DELETE)

- EDIT kucc625; FIND ALL;
- EDIT kucc625; FIND ALL WHERE(gender={'Male'}) INTO x;
- EDIT kucc625; FIND ALL; LIST POINT 7; gender='M'; REPLACE;
- EDIT kucc625; FIND ALL; DELETE; /* delete the current observation */
- EDIT kucc625; FIND ALL; DELETE POINT 7; /* delete the 7th observation */
- EDIT kucc625; FIND ALL; DELETE ALL WHERE (age<=20); /* WHERE clause */

Listing Observations (LIST)

- # SHOW DATASETS; SHOW CONTENTS; CLOSE jeeshim2;
- LIST; LIST CURRENT; LIST ALL;
- LIST ALL VAR{country gnp}; v={country gnp}; LIST ALL VAR v;
- LIST CURRENT VAR{country gnp population} WHERE(gnp>=1000);
- LIST NEXT 5 VAR{country gnp population} WHERE(country=:'K');
- LIST AFTER WHERE(country=*{'Kor', 'South', 'Rep'});
- LIST POINT 7; LIST POINT{1 3 5 7}; p={1 3 5}; LIST POINT p;

Operand

- <, <=, >, >=
- =, ^= /* Equal to; Not equal to */
- ?, ^? /* Contains a given string; Does not contain a given string */
- =: /* Begins with a given string */
- =* /* Sounds like or is spelled similar to a given string */

Transforming to Data Sets and External Files (CREATE)

- CREATE jeeshim2; APPEND;
- CREATE jeeshim2 VAR{gender population}; APPEND; CLOSE jeeshim2
- CREATE jeeshim2 FROM x_matrix; APPEND FROM x_matrix;

#### PROGRAMMING

Module

...

RETURN (results);

FINISH;

- A module is the same as a function of C and other computer language. It begins with START command, ends with FINISH command, and is called by RUN command. We can give arguments to a module and get the results by using RETURN command in a module.
- Calling a Module: RUN module-name (argument1, argument2, ...);

IF THEN/ELSE IF

- IF expression THEN statement1; ELSE IF expression THEN statement2;
- IML uses the symbol | for OR and the symbol & for AND. It will not accept the words as alternatives for logical operators as in the data step.

IF x=3 | x=6 THEN print 'x=' x;

ELSE IF x=4 THEN print 'x is 4';

ELSE print 'x is bad';

IF x=3 | x=6 THEN print 'x=' x;

ELSE IF x=4 THEN print 'x is 4';

ELSE print 'x is bad';

IF x=3 | x=6 THEN print 'x=' x;

ELSE IF x=4 THEN print 'x is 4';

ELSE print 'x is bad';

DO ... END

DO i=1 TO x BY 2;

print i;

END;

DO WHILE ... END

DO WHILE (x<2);

print x; x=x+1;

END;

The DO WHILE loop is evaluated at the top, meaning that if count was 10 in this example, the loop would not execute. The DO UNTIL loop is evaluated at the bottom, meaning that it will always execute at least once.

Do UNTIL ... END

DO UNTIL (x<2);

print x'; x=x+1;

END;

#### MATRIX GENERAL

Definition

- Matrix algebra is useful not only in both simplifying description and promoting development of many analysis methods but also in organizing computer techniques to execute those methods and to present the results.
- A matrix is a rectangular or square array of numbers arranged in rows and columns.

Various Matrices

- A square matrix is a matrix whose number of rows equals the number of columns
- A diagonal matrix is a square matrix whose non-diagonal elements are all zero. D{ai} = diag {ai} for i=1, 2, ..., n.
- The element aij of a matrix A is called the ijth element, the first subscript referring to the row the element is in and the second to the column. Leading element of a matrix is the element in the upper left-hand corner of the matrix. diagonal elements or diagonal of a matrix. off-diagonal or non-diagonal elements.
- The size of the matrix is referred to as its order, or as its dimensions (e.g., Ar X c).
- (Upper or lower) Triangular matrix is a matrix with all elements above (or below) the diagonal being zero.
- A matrix consisting of only a single column is called a column vector (vector). Row vector is a matrix that is just a single row. Single number is called scalar (often represented by lowercase Greek lambda).
- A formal description is that the transpose of a matrix A is the matrix whose columns are the rows of A, with order retained, from first to last (A'). The transpose operation is reflexive: the transpose of a transposed matrix is the matrix itself (i.e., (A')'=A). (AB)'=B'A' and (AA')'=(A')'A=AA'.
- The sum of the diagonal elements of a square matrix is called the trace of the matrix (i.e., tr(A) =the sum of aii). tr(A')=tr(A), tr(A+B)=tr(A)+tr(B), and tr(AB)=tr(BA).
- Matrices should be conformable for their addition and subtraction.
- A null matrix or zero matrix is a matrix whose elements are all zero (0).
- Identity matrix or unit matrix is a diagonal matrix having all diagonal elements equal to unity (Ii).
- Innner product of two column vectors is defined as (A')1 X c Bc X 1=P1 X 1 to produce a scalar and outer product as Bc X 1 (A')1 X c =Pc X c
- For any matrix A, pre or post-multiplication by a null matrix of appropriate order results in a null matrix. Pre-multiplication of a matrix A by a diagonal matrix D gives a matrix whose rows are those of A multiplied by the respective diagonal elements of D.
- A symmetric matrix is defined when A=A', with aij = aji.
- AA'={inner product of i-th and k-th rows of A} for i, k=1, 2, ..., r. And A'A={inner product of j-th and m-th columns of A} for j, m=1, 2, ..., c. A'A=0 implies A=0 and tr(A'A)=0 implies A=0 because every akj should be zero or every element of A is zero if tr(A'A)=the sum of j-th diagonal element of A'A=0. PXX'=QXX' implies PX=QX.
- A orthogonal matrix is defined when AA'=I=A'A

#### INVERSE MATRICES

Determinant

- A determinant is a polynomial of the elements of a square matrix. It is the sum of certain products of the elements of the matrix from which it is derived, each product being multiplied by +1 or -1 according to certain rule.
- The determinant of a 1 X 1 is the value of its sole element (a first-order determinant).
- A second-order determinant is defined by a11 a22 - a12 a21.
- Third order determinant is calculated by,a11a22a33 + a12 a23a31 + a13a21a32 - a11a23a32 - a12a21a33 - a13a22a31.
- Minors (M) --> cofactors (C) --> adjoint (C') --> inverse matrix.

Inverse Matrices

- Cofactors cij are the signed minors, (-1)i+j |Mij|, where Mij is A with its i-th row and j-th column deleted.
- An adjoint matrix is a transpose of cofactor matrix.

Theorem

- [Theorem] The determinant of the transpose of a matrix equals the determinant of the matrix itself (i.e., |A'|=|A|).
- [Theorem] When a non-zero scalar is a factor of a row (column) of |A| then it is also a factor of |A|.
- [Theorem] When a determinant has zero for every element of a row (or column), the determinant is zero.
- [Theorem] Interchanging two rows (columns) of a determinant changes its sign.
- The matrix A-1 is called the reciprocal of A, or inverse of A.
- A-1 = |A|-1C'

#### REFERENCES

- Chiang, Alpha C. 1984. Fundamental Methods of Mathematical Economics (3rd edition). McGraw-Hill.
- Huh, Myoung Hoi, 1990. SAS Regression. Seoul: Freedom Academy.
- SAS Institute. 1990. SAS IML Software: Usage and Reference (Version 6).
- Searle, Shayle R.1982. Matrix Algebra Useful for Statistics
- Wonnacott, Thomas H. and Ronald J. Wonnacott. 1981. Regression: A Second Course in Statistics. Florida: Krieger Publishing Company.