An easy way to define a library of user-defined functions

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.

In my blog posts, I usually define a module in a PROC IML session and then immediately use it. However, sometimes it is useful to store modules so that they are available in future SAS/IML sessions. You can store SAS/IML modules by using the RESET STORAGE statement to define a permanent location and by using the STORE statement to save the modules. Details and examples are presented in my book and in my article "How to Store and Load SAS/IML Modules."

But did you know that you can store and load a bunch of modules at one time by using the _ALL_ keyword?

Let's say that you have a file called UsefulFunctions.sas that contains the following SAS/IML program:

proc iml;
/* Optional: Use RESET STORAGE to set storage location for modules */
start Linear(x);
   return(x);
finish;
 
start Square(x);
   return( Linear(x)#x );
finish;
 
start Cubic(x);
   return( Square(x)#x );
finish;
store module=_all_;
quit;

When you run the program, it defines three modules (Linear, Square, and Cubic) and it stores those modules in compiled form. The _ALL_ keyword enables you to save all currently defined modules without having to specify the list of names. This means that it you one day add a new module (Quartic?) to the UsefulFunctions.sas file, you don't have to remember to include the name of the new module on any list; the new module will be stored automatically when the program is run.

You can use any of those functions at some later time by loading them from storage. You can load them individually, but it is convenient to simply use the _ALL_ keyword as shorthand for "load all of the modules in a specified file," as shown in the following example:

proc iml;
/* Optional: Use RESET STORAGE to specify location of modules */
load module=_all_;
y = Cubic(2);  /* call any function in library */

In this you way you can define related functions in a single file and store them all to a "library" (some languages call these "packages"). For example, you might define a "Utilities" library that has general purpose functions and a "Multivariate" library that includes functions related to multivariate analysis. When you want to use some of these functions, the _ALL_ keyword makes it easy to load all functions in a library.

To summarize, here's how you can create a library of SAS/IML functions:

  1. Define the location of the function library by using the RESET STORAGE statement, as described in the article "How to Store and Load SAS/IML Modules."
  2. Run a SAS/IML statements that define all the functions to include in the library. (The easy way: Use the %INCLUDE statement, such as %include UsefulFunctions;)
  3. Run store modules=_all_;

When you want to use the functions in the library, do the following:

  1. Define the location of the function library by using the RESET STORAGE statement.
  2. Run load modules=_all_;

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