Lesson 31:  Introduction to IML


SAS IML is a programming language for working with matrices.  IML stands for "Interactive Matrix Language."  The language is invoked, or started, by issuing a "proc iml;" command.  IML is interactive.  You can keep submitting statements, one after the other, and IML will execute them.  No "run" statement is necessary.  IML stops when another step (data or proc) starts, or a "quit;" command is given.

Video:  Defining Matrices, Operators, and Functions

We can define a matrix in IML with an assignment statement and a list of elements in curly brackets, with rows separated by commas.  Just like any other SAS code, the placement of the text on the line doesn't matter, so you can string them out on one line or organize them neatly in rows and columns.  In order to see what the matrix looks like, you can use the print command.

The standard matrix operations, line addition, subtraction, and multiplication, are given by the usual operators, "+", "-", and "*".   The division operator can be used with scalars, but if used with two conformable matrices, it will do element-wise division too, and a double asterisk is for exponents, as usual.  However, there are many more operators in IML.  The number sign ("#") is element-wise multiplication, and a double number sign is element-wise exponentiation.  Double horizontal bars will concatenate matrices side-by-side, while double slashes will concatenate them vertically.

There are a number of standard functions that are commonly used in IML.  The transpose of X is given by "X`", which is X followed by a back-quote character (to the left of "1" on the keyboard).  It can also be found by "t(X)" which is the function notation equivalent.  The inverse of X is given by "inv(X)".  Since there are many functions, the best thing to do is refer to the documentation.  In the Online Documentation, IML has its own entry in the main tree.  You can go to the link http://support.sas.com/onlinedoc/913/docMainpage.jsp, then find the branch that says "SAS/IML User's Guide.  Click the plus sign next to it, and a long list of interesting branches open up.  Near the bottom, we find "Language Reference."  Open that up, and you will find, among other things, "Operators" and "Statements, Functions, and Subroutines."  In these two sections you will find information about the operators and functions.  There is no need to repeat all this here, you should make use of the documentation as necessary.



1. Explore the available functions and give examples of the use of five different functions.

Copyright reserved by Dr.  Dwight Galster, 2009.  Please request permission for reprints (other than for personal use) from dwight@galsterhome.com .  "SAS" is a registered trade name of SAS Institute, Cary North Carolina.  All other trade names mentioned are the property of their respective owners.  This document is a work in progress and comments are welcome.  Please send an email if you find it useful or if your site links to it.