SAS/IML - Interactive Matrix Language
SAS/IML - Interactive Matrix Language
v SAS/IML is interactive
v commands can be executed as entered or
v commands can be collected in a module and executed later
10-1
SAMPLE IML SESSION
1 2 3
1
2
3
1 2 3
4 5 6
10-2
DEFINING A MATRIX
v The fundamental data object on which all IML commands operate is a two-dimensional
numeric or character matrix
v The dimension of a matrix is defined by the number of rows and columns it has. An m X n
matrix has mn elements arranged in m rows and n columns
10-3
ROW VECTOR 1 X N
1 2 3
COLUMN VECTOR M X 1
1
2
3
SCALAR 1 X 1
M X N MATRIX
1 2 3
4 5 6
7 8 9
10-4
MATRIX NAMES and MATRIX LITERALS
A) Matrix Names
- a matrix is referred to by a valid SAS name
- naming convention rules are the same as in Base SAS
- a name is associated with a matrix when the matrix is created
- the type(character or numeric), dimension, or values of a matrix can be changed at any time
B) Matrix Literals
- when you represent a matrix by a literal, you specify the values of each element of the
matrix.
- matrix literals can have a single element or have many elements arranged in rectangular
form.
- if there are multiple elements, use braces({ }) to enclose the values and commas
to separate the rows.
- within the braces, values must be either all numeric or all character.
- all rows must have the same number of elements.
C) Scaler Literals
a=12 ;
a=. ;
a='bios 111'
a="BIOS111" ;
D) Numeric Literals
x={1 2 3 4 5 6} ;
y={1,2,3,4,5} ;
z={1 2, 3 4, 5 6} ;
E) Character Literals
10-5
a={abc defg} ;
A
ABC DEFG
a={'abc' 'DEFG'}
A
abc DEFG
F) Reassigning Values
a={1 2 3, 4 5 6} ;
The statement
a={'Gonzo' 'Piggy' } ;
10-6
Scaler Literals Example
84 PROC IML ;
IML Ready
85 RESET PRINT ;
86
87 A = 12 ;
88 B = . ;
89 C = 'bios 111' ;
90 d = "BIOS111" ;
91
92 QUIT ;
Exiting IML.
NOTE: The PROCEDURE IML used 0.71 seconds.
SCALER LITERALS
12
bios 111
BIOS111
10-7
Numeric Literals Example
93 TITLE "NUMERIC LITERALS" ;
94 PROC IML ;
IML Ready
95 RESET PRINT ;
96
97 X = {1 2 3 4 5 6} ;
98 Y = {1,2,3,4,5} ;
99 Z = {1 2, 3 4, 5 6} ;
100
101 QUIT ;
Exiting IML.
NOTE: The PROCEDURE IML used 4.0 seconds.
NUMERIC LITERALS
1 2 3 4 5 6
1
2
3
4
5
1 2
3 4
5 6
10-8
Character Literals Example
93 TITLE "CHARACTER LITERALS" ;
94 PROC IML ;
IML Ready
95 RESET PRINT ;
96
97 A = {abc defg} ;
98 B = {‘abc’‘defg’} ;
99 B = {1 2 3, 4 5 6} ;
100
101 QUIT ;
Exiting IML.
NOTE: The PROCEDURE IML used 4.0 seconds.
CHARACTER LITERALS
ABC DEFG
abc defg
1 2 3
4 5 6
10-9
IML: ASSIGNMENT STATEMENTS
Assignment statements create matrices by evaluating expressions and assigning results to a matrix
result= expression ;
where result is the name of a new matrix and expression is an expression that is evaluated, the
results of which is assigned to a new matrix
OPERATORS
prefix operators are placed in front of operands . For example, -A uses the sign reverse prefix
operator in front of the operand A to reverse the sign of each element of A.
infix operators are placed between operands. For example, A+B uses the addition infix operator (+)
.
postfix operators are placed after an operand. For example, A` uses the transpose postfix operator
after the operand A to transpose A.
10-10
Table A1.1 Operators
Operator Action
+ Addition
# Multiplication
<> Maximum
>< Minimum
<:> Index of maximum
>:< Index of minimum
: Mean
## Sum of squares
10-11
EXAMPLES:
LET A= 2 2 and B= 4 5
3 4 1 0
A + B yields 6 7 addition
4 4
A # B yields 8 10 elementwise
3 0 multiplication
A * B yields 10 10 matrix
16 15 multiplication
A` 2 3 transpose operator
2 4
A || B yields 2 2 4 5 horizontal
3 4 1 0 concatenation
A // B yields 2 2 vertical
3 4 concatenation
4 5
1 0
10-12
Operators Example
OPERATORS
2 2
3 4
4 5
1 0
-4 -5
-1 0
10-13
D 2 rows 2 cols (numeric)
6 7
4 4
2 3
2 4
IML: SUBSCRIPTS
Subscripts are special postfix operators placed in square brackets([ ]) after a matrix operand.
Subscript operators have the general form:
operand[row,column]
where
10-14
SELECTING A SINGLE ELEMENT
You can select a single element in two ways. You can use two subscipts (row,column), or you can
use 1 subscript to look for the element down the rows.
For example:
LET A= 1 2 3
4 5 6
7 8 9
B=A[2,3] yields 6
You can also look for an element down the rows. In this case you would refer to this element as the
sixth element of A .
B=A[6] ;
To select an entire row or column, write the subscript with the row or column number, omitting the
other subscript but not the comma.
C=A[1,] ; C
1 2 3
To select column 2 of A.
D=A[,2] ; D
2
5
8
10-15
SUBMATRICES
You can refer to submatrix by the specific rows and columns that you want. Include within the
brackets the row you want, a comma, and the columns you want.
To create a submatrix consisting of the first and third rows and the first and second columns of A:
E=A[{1 3},{1,2}] ; E
1 2
7 8
You can use the index creation operator(:) to refer to successive rows or columns. For example, to
create a submatrix consisting of rows 1-3 and columns 1-2 from B :
LET B= 1 2 3 4
5 6 7 8
9 10 11 12
F=B[1:3,1:2] ; F
1 2
5 6
9 10
SUBSCRIPT ASSIGNMENT
You can assign values into a matrix using subscripts to refer to the element or submatrix. For
example to change the value in the first row, second column of A from 2 to 30:
A[1,2]=30;
A[,3]={20 30 40} ; A
1 2 20
4 5 30
7 8 40
10-16
Subscript Reduction Operators
You can use subscript reduction operators to perform operations across all rows or columns. Using
these operators will result in a matrix of reduced dimensions. For example, subscript reduction
operators can be used to obtain column sums, row sums, or column or row means. To get column
sums of the matrix X (sum across the rows, which reduces the row dimension to 1), specify X[+,1].
The first subscript (+) indicates summation reduction takes place across the rows. Omitting the
column subscript, leaves the column dimension unchanged.
These operators can be used to reduce rows, columns, or both. When both rows and columns are
reduced, rows are reduced first. Table A1.2 lists the eight operators for subscript reduction.
PROC IML;
RESET PRINT ;
A={0 1 2,5 4 3,7 6 8} ;
B=A[,+] ;
C=A[,:] ;
D=A[+,] ;
E=A[:,] ;
QUIT;
0 1 2
5 4 3
7 6 8
3
12
21
10-17
C 3 rows 1 col (numeric)
1
4
7
12 11 13
4 3.6666667 4.3333333
FUNCTIONS AS EXPRESSIONS
Matrices can also be created as a result of a function call. Scalar functions such as LOG or SQRT
operate on each element of the matrix, while matrix function such as INV or RANK operate on the
entire matrix. The general form of a function is:
result=FUNCTION(arguements) ;
For example:
A=SQRT(B) ;
10-18
MATRIX GENERATING FUNCTIONS
BLOCK(matrix1,matrix2,...) ;
1 1
1 1
2 2
2 2
1 1 0 0
1 1 0 0
0 0 2 2
0 0 2 2
THE J FUNCTION
J(nrow,rcol,value) ;
creates a matrix with nrow rows, ncol columns, and all element
values equal to value
10-19
C = J(2,2,1) ; RESULTS IN THE MATRIX
1 1
1 1
THE I FUNCTION
I(dimension) ;
1 0 0
0 1 0
0 0 1
NCOL(matrix) ;
B=NCOL(A) ;
10-20
THE NROW FUNCTION
NROW(matrix) ;
B=NROW(A) ;
ABS(matrix) ;
C=ABS(A) ;
SQRT(matrix) ;
C=SQRT(A) ;
10-21
THE EXP FUNCTION
EXP(matrix) ;
B={2 3 4} ;
LOG(matrix) ;
C=LOG(A) ;
MAX(matrix1,matrix2, ...matrix15) ;
10-22
An example of how to use the MAX function is
C=MAX(A) ;
DET(matrix) ;
C=DET(A) ;
INV(matrix) ;
C=INV(A) ;
DIAG(matrix) ;
10-23
If matrix is a square matrix, the DIAG function creates a matrix
with diagonal elements equal to the corresponding diagonal
elements. All off-diagonal elements in the new matrix are zeros.
For example,
A={4 3,
2 1} ;
C=DIAG(A) ; results in
4 0
0 1
B={1 2 3} ;
D=DIAG(B) ; results in
1 0 0
0 2 0
0 0 3
VECDIAG(matrix) ;
10-24
For example
A={4 3,
2 1} ;
C=DIAG(A) ; results in
4
1
10-25
Functions Example
PROC IML;
RESET PRINT ;
USE OUT;
READ ALL INTO VB ; /* betas/variance covariance matrix */
NVB= NROW(VB); /* number of rows in VB */
BETA=VB[1,]; /* row vector or betas */
BETA=BETA`; /* convert to column vector of betas*/
V=VB[2:NVB,]; /* variance covariance matrix */
V=VECDIAG(V) ; /* column vector of variances */
S=SQRT(V) ; /* column vector of standard errors */
L=BETA - (1.96#S) ; /* lower 95% CI */
H=BETA + (1.96#S) ; /* upper 95% CI */
LBH = BETA || L || H ; /* lower CI, betas, upper CI */
QUIT;
10-26
IML Output
2.10335
0.01019
-0.01004
0.00023
-0.02835
10-27
V 5 rows 1 col (numeric)
2.94187
0.00014
0.00002
0
0.00016
1.715188
0.0118322
0.0044721
0
0.0126491
-1.258419
-0.013001
-0.018805
0.00023
-0.053142
5.4651186
0.033381
-0.001275
0.00023
-0.003558
10-28
WORKING WITH SAS DATA SETS
v SAS/IML software can pass data from SAS data sets to matrices and from matrices to SAS data
sets.
v You can create matrices from the variables and observations in several ways. You can create a
column vector for each data set variable, or you can create a matrix where columns correspond
to data set variables.
v You can create a SAS data set from a matrix. The columns correspond to data set variables and
the rows correspond to observations.
v Before you can access a SAS data set, you must first open it. There are three ways to open a
SAS data set:
v To read from an existing SAS dataset, submit a USE command to open it. The general form of
the USE statement is:
v To read and write to an existing SAS data set, use the EDIT statement. The general form of
the EDIT statement is:
v To create a new SAS dataset, use the CREATE statement. The general form of the CREATE
statement is:
v Use the APPEND statement to place observation into the newly created data set. If you don't
use the APPEND statement, the new data set will have no records.
v Transferring data from a SAS data set to a matrix is done with the READ statement.
v The SAS data set that you want to read from must already be open.
10-29
v You can open a SAS data set with the USE or EDIT statement.
where
v Each variable in the VAR clause becomes a column vector with the same name as the variable
in the SAS data set.
v For example, to read the variables AGE, HEIGHT, and WEIGHT for all observations in
CLASS, use the statements
USE classlib.class ;
READ all VAR{age height weight} ;
The read statement above creates 3 numeric vectors AGE, HEIGHT, and WEIGHT. The two
statements above can also be written as:
10-30
USING THE READ STATEMENT WITH THE VAR & INTO CLAUSES
v You can use the READ statement with the VAR and INTO clauses to read the variables listed
in the VAR clause into a single matrix named in the INTO clause.
v Each variable in the VAR clause becomes a column in the target matrix. If there are p variables
in the var clause and n observations processed, the target matrix in the INTO clause is an nXp
matrix.
v The following creates a matrix X containing all numeric variables in the CLASS data set.
USE classlib.class ;
READ all VAR _num_ INTO X ;
You can use the WHERE clause to conditionally select observations from within the specified
range. IF you want to create a matrix FEMALE containing AGE, HEIGHT, and WEIGHT for
females only:
USE classlib.class ;
READ all VAR _num_ INTO female WHERE(sex="F");
v The CREATE and APPEND statements can be used to create a SAS data set from a matrix.
v The columns of the matrix become the data set variables and the rows of the matrix become the
observations.
v An nXm matrix creates a SAS data set with m variables and n observations.
v The CREATE statement opens the new SAS data set for both input and output, and the
APPEND statement writes to the SAS data set.
10-31
USING THE CREATE STATEMENT WITH THE FROM OPTION
You can create a SAS data set from a matrix using the CREATE statement with the FROM
option. This form of the CREATE statement is:
where
Suppose you want to create a SAS data set named RATIO containing a variable with the height-to-
weight ratios for each student and the ratio is stored in a one column matrix called HTWT. You can
use the CREATE and APPEND statements to open a new SAS data set called RATIO and append
the observations, naming the data set variable HTWT instead of COL1.
You can create a SAS dataset from a matrix using the CREATE statement with the VAR option.
This form of the CREATE statement is:
where operand are the variables to be included in the SAS data set
For example the following statements create a new SAS data set CLASS having variables NAME,
SEX, AGE, HT, and WT. The data come from IML matrices having the same name.
you could not do the above using the FROM option because NAME and SEX are character, while
AGE, WT, and HT are numeric.
10-32
Working With SAS Data sets: Examples
20 PROC IML ;
IML Ready
21 USE SC.CLASS ;
23 ** LIST ALL OPEN DATASETS ** ;
25 SHOW DATASETS ;
27 ** CONTENTS OF ALL OPEN DATASETS ** ;
29 SHOW CONTENTS ;
31 READ ALL VAR{AGE HT WT} ;
33 ** LIST ALL MATRICES CREATED SO FAR ** ;
35 SHOW NAMES ;
36
37 QUIT ;
Exiting IML.
NOTE: The PROCEDURE IML used 0.32 seconds.
DATASET : SC.CLASS.DATA
10-33
Working With SAS Datasets: Examples
38 PROC IML ;
IML Ready
39 USE SC.CLASS ;
40
41 READ ALL VAR _NUM_ INTO X ;
42
43 ** PRINT X ** ;
44
45 PRINT X ;
46
47 ** CLOSE SC.CLASS ** ;
48
49 CLOSE SC.CLASS ;
50 QUIT ;
Exiting IML.
NOTE: The PROCEDURE IML used 4.78 seconds.
_________________________________________
X
37 71 195
31 70 160
41 74 195
. 48 .
3 12 1
14 25 45
10-34
Working With SAS Data Sets: Examples
69 PROC IML ;
IML Ready
70 USE SC.CLASS ;
71
72 READ ALL VAR _NUM_ INTO X WHERE(SEX='M');
73
74 ** CREATE MATRIX CONTAINING VARIABLE **
** NAMES ** ;
75
76 NAMES={"AGE" "HT" "WT"} ;
77
78 ** PRINT X ** ;
79
80 PRINT X[COLNAME=NAMES FORMAT=6.2] ;
81
82 ** CLOSE SC.CLASS ** ;
83
84 CLOSE SC.CLASS ;
85 QUIT ;
Exiting IML.
NOTE: The PROCEDURE IML used 2.37 seconds.
_________________________________________
X AGE HT WT
10-35
Working With SAS Datasets: Examples
_________________________________________
CLASS DATESET
X AGE HT WT
10-36
Working With SAS Datasets: Examples
36 PROC IML ;
37 USE SC.CLASS ;
38 READ ALL ;
39 HTWT=HT/WT ;
40 SHOW NAMES ;
41 CREATE RATIO FROM HTWT[COLNAME='HTWT'];
42 APPEND FROM HTWT;
43 SHOW DATASETS ;
44 SHOW CONTENTS ;
45 CLOSE RATIO ;
NOTE: The data set WORK.RATIO has 6 observations and 1 variables.
46 QUIT ;
Exiting IML.
NOTE: The PROCEDURE IML used 0.66 seconds.
_________________________________________
DATASET : WORK.RATIO.DATA
10-37
Working With SAS Datasets: Examples
47 PROC IML ;
48 USE SC.CLASS ;
49 READ ALL VAR{NAME SEX HT WT} ;
50 SHOW NAMES ;
51 CREATE RATIO2 VAR{NAME SEX HT WT};
52 APPEND ;
53 SHOW DATASETS ;
54 SHOW CONTENTS ;
55 CLOSE RATIO2 ;
NOTE: The data set WORK.RATIO2 has 6 observations and 4 variables.
56 QUIT ;
Exiting IML.
NOTE: The PROCEDURE IML used 7.96 seconds.
_________________________________________
DATASET : WORK.RATIO2.DATA
10-38
OTHER EXAMPLES
3X1 - X2 + 2X3 = 8
2X1 - 2X2 + 3X3 = 2
4X1 + X2 - 4X3 = 9
3 -1 2 X1 8
2 -2 3 X2 = 2
4 1 -4 X3 9
AX = C or
X = A-1 C
5 PROC IML;
IML Ready
6 RESET PRINT ;
7 A={3 -1 2,
8 2 -2 3,
9 4 1 -4} ;
10
11 C={8, 2, 9} ;
12
13 X=INV(A)*C ;
14 QUIT ;
Exiting IML.
NOTE: The PROCEDURE IML used 4.67 seconds.
10-39
A 3 rows 3 cols (numeric)
3 -1 2
2 -2 3
4 1 -4
8
2
9
3
5
2
Y = XB + E
B = (X`X)-1*X`*Y ;
Y = B1X1 + B2X2
10-40
28 PROC IML;
29 RESET PRINT ;
30 X={1 1 1,
31 1 2 4,
32 1 3 9,
33 1 4 16,
34 1 5 25} ;
35 Y={1, 5, 9, 23, 36} ;
36 B=INV(X`*X)*X`*Y ;
37 QUIT ;
Exiting IML.
1 1 1
1 2 4
1 3 9
1 4 16
1 5 25
1
5
9
23
36
2.4
-3.2
2
10-41