Programming Statements


Understanding Argument Passing

You can use expressions and subscripted matrices as arguments to a module, but it is important to understand the way the SAS/IML software passes the results to the module. Expressions are evaluated, and the evaluated values are stored in temporary variables. Similarly, submatrices are created from subscripted variables and stored in temporary variables. The temporary variables are passed to the module. In the following example, notice that the matrix x does not change; you might expect x to contain the squared values of y.

start Square(a,b);
   a = b##2;
finish;

x = {. .};                 /* initialize with missing values     */
y = {3 4};
reset printall;            /* print all intermediate results     */
do i = 1 to 2;             /* pass elements of matrix to modules */
   run Square(x[i],y[i]);  /* WRONG: x[i] is not changed         */
end;
print x;                   /*  show that x is unchanged          */

The output is shown in Figure 6.14. The names of the temporary matrices created by the subscript operators are _TEM1001 and _TEM1002. These are the matrices passed into the square module. The module assigns the value 9 to the local matrix a, and this value is returned to main scope in the temporary matrix _TEM1001, which promptly vanishes! The same sequence of operations repeats for the next call to the Square module.

Figure 6.14: Temporary and Local Matrices in a Module

i 1 row 1 col (numeric)

1

_TEM1002 1 row 1 col (numeric)

3

_TEM1001 1 row 1 col (numeric)

.

a 1 row 1 col (numeric)

9

_TEM1002 1 row 1 col (numeric)

4

_TEM1001 1 row 1 col (numeric)

.

a 1 row 1 col (numeric)

16



Consequently, the values of x remain unchanged by the previous calls, as shown in Figure 6.15. The lesson to learn from this example is this: do not pass in an expression or literal as an output argument to a module. Use only matrix names for output arguments. For example, the correct way to call the Square module is to eliminate the loop and simply use the statement run Square(x, y);.

Figure 6.15: An Unchanged Matrix

x
. .