Everything you wanted to know about writing SAS/IML modules

3

One of the fundamental principles of computer programming is to break a task into smaller subtasks and to modularize the program by encapsulating each subtask into its own function. I have written many blog posts over the years about how to define and use functions in the SAS/IML language. I thought it would be useful to compile that information in a single place so that SAS/IML programmers can find it easily.

I will periodically add material to this post so that it remains current.

A simple example

The purpose of SAS/IML software is to enable you to extend the capabilities of SAS software. Creating user-defined modules is an important part of that purpose. Suppose that you want to define a new function that squares each element of a matrix. The following statements define the function Sqr and call the function with a vector parameter:

proc iml;
/* square each element of a matrix */
start Sqr(x);
   return( x#x );  /* elementwise multiplication */
finish;
 
x = 1:5;           /* x = {1 2 3 4 5} */
y = Sqr(x);        /* square each element: y = {1 4 9 16 25} */

How to define and use SAS/IML function modules

The following list contains fundamental information about creating SAS/IML functions.

  • It's easy to define a new function in SAS/IML software. Use the START statement to specify the function name and arguments, write the body of the function, and use the FINISH statement to signal that the definition is complete.
  • The parameters to a SAS/IML function are passed by reference, which means that their values can be modified from within the body of the function.
  • There are two kinds of modules in the SAS/IML language: functions and subroutines. Functions use the RETURN statement to return a value. Subroutines do not return a value. They typically make modifications to the arguments (for example, sorting a vector), although you can also use subroutines to return multiple arguments from a module.
  • Traditionally, the arguments to a user-defined module are matrices and the module returns the results in one or more matrices. Alternatively, you can pack the arguments into a SAS/IML list and send the list to the module. Similarly, you can pack many results into a list and return it as an output argument.

How to save, reuse, and share SAS/IML functions

An advantage of writing a module is that you can call the same function from several programs.

Optional parameters and default values

SAS/IML 12.1 and 12.3 introduced optional parameters and default values for parameters.

Scope of functions and variables

The following list provides technical details about how modules work.

Debugging tips

Share

About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

3 Comments

  1. Pingback: On passing a list to a SAS/IML module - The DO Loop

Leave A Reply

Back to Top