Programming Statements


Modules with Arguments

Most modules contain one or more arguments, and therefore contain a separate local symbol table. The following statements apply to modules with arguments:

  • You can specify arguments as variable names, expressions, or literal values.

  • If you specify several arguments, use commas to separate them.

  • Arguments are passed by reference, not by value. This means that a module can change the value of an argument. An argument that is modified by a module is called an output argument.

  • If you have both output arguments and input arguments, the SAS/IML convention is to list the output arguments first.

  • When a module is run, the input arguments can be a matrix name, expression, or literal. However, you should specify only matrix names for output arguments.

When a module is run, the value for each argument is transferred from the global symbol table to the local symbol table. For example, consider the module Mod2 defined in the following statements:

proc iml;
a = 10;
b = 20;
c = 30;
start Mod2(x,y);                     /* begin module */
   p = x+y;
   x = 100;                          /* change the value of an argument */
   c = 25;
finish Mod2;                         /* end module   */

run Mod2(a,b);
print a b c;

The first three statements are submitted in the main scope and define the variables a, b, and c. The values of these variables are stored in the global symbol table. The START statement begins the definition of Mod2 and lists two variables (x and y) as arguments. This creates a local symbol table for Mod2. All symbols used inside the module (x, y, p, and c) are in the local symbol table. There is also a correspondence between the arguments in the RUN statement (a and b) and the arguments in the START statement (x and y). Also note that a and b exist only in the global symbol table, whereas x, y, and p exist only in the local symbol table. The symbol c exists in both symbol tables, but the values are completely independent.

When Mod2 is executed with the RUN statement, the local variable x becomes the "owner" of the data in the global matrix a. Similarly, the local variable y becomes the owner of the data in b. Because c is not an argument, there is no correspondence between the value of c in the global table and the value of c in the local table. When the module finishes execution, the variables a and b at main scope regain ownership of the data in x and y, respectively. The local symbol table that contains x and y is deleted. If the data were modified within the module, the values of a and b reflect the change, as shown in FigureĀ 6.4.

Figure 6.4: Output from Module with Arguments

a b c
100 20 30



Notice that after the module is executed, the following are true:

  • a is changed to 100 since the corresponding argument, x, was changed to 100 inside the module.

  • b is still 20.

  • c is still 30. Inside the module, the local symbol c was set to 25, but there is no correspondence between the global symbol c and the local symbol c.

Also note that, inside the module, the symbols a and b do not exist. Outside the module, the symbols p, x, and y do not exist.