Tips for the IML Procedure in SAS
Posted on Jul 19, 2012 in Computer Science
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
-
End an
IML
procedure withquit;
. You should never userun;
to end an IML procedure because it is used for calling user-defined functions in IML procedures. -
Until SAS 9.3, the
IML
procedure does not support overloading functions or recursive functions. -
You can define functions in the
IML
procedure, and the definition can be nested. That is you can define another function in a user-defined function. Note that the order of definition of functions matter (similar to C). A function must be defined before it is called. -
In the
IML
procedure, every data object is a matrix. There is no concept of vector in the IML procedure like R does. However, elements of a matrix can be accessed by a single index (e.g., x[3]), so you can a matrix with 1 row/column as if it is a vector (as in R). When a matrix has multiple rows and columns, you can still access its elements using a single index, but be aware that elements are read by ROW which is different from R. For example, the following code prints out 2 and 10.
proc iml;
x = {1 2 3, 10 100 1000};
m = nrow(x);
n = ncol(x);
print (x[2]);
print(x[4]);
quit;
-
In SAS, data sets are global which means that any procedure can use a created data set. However, variables defined in a
IML
procedure is local, which means that variables defined in differentIML
procedures are not shared. -
You can define subroutines in a
IML
procedure. A subroutine is just a bunch of statment putted together. A subroutine does not have any argument, which means that it must invoked without parentheses. A module/function can have argument(s), and must be invoked with parentheses. Another important difference is that variables defined in a function takes effect only in the function while variables in a module takes effect in the wholeIML
procedure after its definition. Based on these comparision, function is more useful and better than module in theIML
procedure. -
The slicing of vectors and matrices in the IML procedure is similar to that in R.
-
The name of a function in the
IML
procedure can exceeds 8 characters, but I am not sure about the maximum length. -
You can use
mattrib
to modify attributes of matrix in theIML
procedure. -
You cannot print value of an expression directly in the IML procedure. You must either assign the value of the expression to a variable first or put the expression into parentheses. For example, proc iml; print 1+2; quit; raises an error while the following two ways of coding works. proc iml; print (1+2); quit; proc iml; x = 1+2; print x; quit;
-
You cannot use the
put
command to print value of variables in the IML procedure, instead, you should use theprint
command. However, unlikeput
,print
cannot format (e.g., date9.) output values. -
You can
||
to combine matrix horizontally and//
to combine matrix vertically. -
You can define a same module multiple times in an IML procedure. The last defintion will be used.
-
By default, modules defined in an IML procedure is not visible to another IML procedure. However, you can store modules (to a library) defined in an IML procedure using the statement store module=LocNonMissingRows; And you probably want to change the library for storing modules to a permanent one before you storing a module. You can do this using reset storage=BlogDir.BlogModules; You can then use a stored module in aother IML procedure by loading it first. load module=LocNonMissingRows;
-
You can read files using the
%include
command in an IML procedure. This means that you can define modules and put them into sas files and read them in when needed. This makes your SAS code more resuable and easier to manage. You can also store user-defined modules and load them to be accessible later. However, it is not as convenient as%include
. And store a module to a permannet location make it inconvenint to update these moduels. It is suggested that you always use%include
to read definition of modules from files.
Questions
- not very sure how to use default arguments in user-defined functions in the
IML
procedure.