Did you know that PROC IML automatically loads certain modules?

0

In the SAS/IML language, a user-defined function or subroutine is called a module. Modules are used to extend the capability of the SAS/IML language. Usually you need to explicitly load modules before you use them, but there are two cases where PROC IML loads a module automatically.

Modules in IMLMLIB are automatically loaded

There are certain modules that are distributed with SAS/IML software in a library called IMLMLIB. The IMLMLIB library contains some useful functions, such as the ROWVEC and COLVEC function and the RANDNORMAL module, which samples data from a multivariate normal distribution. These modules are loaded automatically when they are called, as shown in the following example:

proc iml;
y = {1,2,3,4,5,6}; /* column vector */
x = rowvec(y); /* convert to row vector */

The SAS log displays the following note:

   NOTE: Module ROWVEC loaded from the storage SASHELP.IMLMLIB.

Modules in the currently-open storage library are automatically loaded

After you load a module from a storage library, that library remains open. You can call other modules in the same storage library. These modules are loaded "on demand" when they are needed.

For example, suppose that you define and store the following four modules:

proc iml;
start Linear(x);
   return(x);
finish;
start Square(x);
   return( Linear(x)#x ); 
finish;
start Cubic(x);
   return( Square(x)#x );
finish;
start Quartic(x);
   return(x#x#x#x);
finish;
store module=_all_;
quit;

Notice that the Cubic function calls the Square function, and the Square function calls the Linear function. The Quartic function does not call any other function.

You can use any of those functions at some later time by loading them from storage. For example, the following program loads the Cubic module::

proc iml;
load module=Cubic;

When you execute these statements, the SAS log displays the fact that it opened the current storage library:

   NOTE: Opening storage library WORK.IMLSTOR

Suppose that the next thing you want to do is to call the Cubic function, as follows:

y = Cubic(2);

What will happen? Remember, you loaded only the Cubic module. However, the Cubic function calls the Square function, which has not been loaded!

No worries. When PROC IML tries to run the Cubic function, it realizes that it needs to call the Square function and looks in the currently open storage library. There it finds the Square function, so it automatically loads the function and then calls it. But when it calls the Square function, it realizes that the Linear function is also needed! No problem: that function is also automatically loaded. These events are recorded in the SAS log:

   NOTE: Module SQUARE loaded from the storage WORK.IMLSTOR.
   NOTE: Module LINEAR loaded from the storage WORK.IMLSTOR.

In a similar way, you can call the Quartic function because it is contained in the storage library that is currently open:

q = Quartic(2);
   NOTE: Module QUARTIC loaded from the storage WORK.IMLSTOR.

To summarize, although you usually need to load a stored module before you can call it, there are two exceptions: when the module is in the IMLMLIB library, and when the called module is found in the currently open storage library.

In SAS/IML Studio, you NEVER need to load modules!

This article is primarily concerned with PROC IML. SAS/IML Studio, the development environment for SAS/IML programmers, makes it even easier to store, load, and manage modules. The SAS/IML Studio application has a dialog box (Tools > Options) that you can set up Windows search paths that are automatically searched for modules. For more information on how SAS/IML Studio stores and loads modules, use the Help that is available from within the SAS/IML Studio application: choose Help > Help Topics, then select the topics SAS/IML Studio Help > The IMLPlus Language > IML Language Elements > Matrix and Module Storage.

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.

Leave A Reply

Back to Top