Local functions (not!) in the SAS/IML language


I have previously written about the scope of local and global variables in the SAS/IML language. You might wonder whether SAS/IML modules can also have local scope.

The answer is no. All SAS/IML modules are known globally and can be called by any other modules. Some object-oriented programming languages support "private" functions (methods) that are callable only from within the class in which they are defined, but the classic SAS/IML language does not support private functions.

Occasionally a question appears on a discussion forum about how to define a private function. (Other terms include "nested modules," "local functions," or "nested scope.") I think the source of some people's confusion is that there is an example in the SAS/IML documentation that shows that while you are in the middle of defining a module, you can define a second module. The example is similar to the following:

proc iml;
start SumOfSquares(n);      /* start defining SUMOFSQUARE function */
   start Sqr(i);            /* take a detour: define SQR function */
   finish Sqr;              /* definition of SQR function complete */
   return( sum(Sqr(1:n)) ); /* Use SQR in body of SUMOFSQUARES */
finish SumOfSquares;        /* definition of SUMOFSQUARES complete */
y = SumOfSquares(5);
print y;

The program uses the START statement to define the syntax for the SUMOFSQUARES function. But before the definition of the function is completed, the program changes direction and defines the SQR function. After the SQR function is defined, it is used in the body of the SUMOFSQUARES function. Lastly, the FINISH statement completes the definition of the SUMOFSQUARES function.

The indentation makes it look like the SQUARE function is local to the SUMOFSQUARES function, but that is not true. The SQR function is a global function, as shown by the following statements:

s = Sqr(10);
print s;
show modules;

The output shows that the SQR function is callable from main scope. The output from the SHOW statement also demonstrates that the SQR function is defined at main scope. The exact same result is acheived if the program defines the SQR function first, then defines the SUMOFSQUARES function afterwards.

So remember: although the SAS/IML language supports local and global variables, module are always known globally within a program. There is no such thing as a local module.

If you want to combine object-oriented programming with SAS/IML syntax, you might be interested in IMLPlus, the programming language of the SAS/IML Studio application. In IMLPlus you can use Java classes, which support private methods. For more on object-oriented programming in the IMLPlus language, see Chapter 6 of the book Statistical Programming with SAS/IML Software or browse the online IMLPlus documentation. SAS/IML Studio is included at no charge with every SAS/IML license, so anyone who has SAS/IML software can install and use SAS/IML Studio.


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 PROC IML and SAS/IML Studio. 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