Programming Statements


Passing Arguments by Using Keyword-Value Pairs

The standard way to pass arguments to a module is to supply a comma-separated list of parameter values that is enclosed in parentheses. In this syntax, the arguments are supplied as positional parameters. You can call module subroutines by using an alternative syntax, called the keyword-value pairs syntax (also known as the named arguments syntax and the keyword parameters syntax).

When you define a module, the names of the parameters become keywords for calling that module. You can call the module by specifying some of the positional parameters and then specifying the remaining parameters as keyword-value pairs outside parentheses. For example, the Mod2 module that is defined in the previous section has two parameters, named x and y. The following calls are equivalent:

run Mod2(a,b);          /* two positional parameters              */
run Mod2(a)   y=b;      /* one positional parameter; one keyword  */
run Mod2()    x=a y=b;  /* no positional parameters; two keywords */
run Mod2()    y=b x=a;  /* order of keywords does not matter      */

Although the order of positional parameters is important, you can specify keyword-value pairs in any order, as shown in the last statement.

Keyword-value pairs are especially useful for modules that have a large number of optional arguments (see the section Modules with Optional and Default Arguments). For example, if a module accepts five optional parameters and you want to supply a value for the fifth, it is easier to use keyword-value pairs than to use positional parameters, as shown in the following example:

start Sum5(x, a=1, b=2, c=3, d=4, e=5);
   x = a + b + c + d + e;
finish;

run Sum5(x,,,,,10);  /* skip some positional parameters    */
run Sum5(x) e=10;    /* equivalent, but simpler            */
print x;

Figure 6.5: Specifying a Keyword-Value Pair

x
20



You can also use keyword-value pairs to specify the optional parameters for many of the SAS/IML built-in subroutines. For an example, see the QUAD subroutine .

The ability to pass arguments to modules by using keyword-value pairs is supported only for subroutines. Function modules, which are described in the next section, do not support keyword-value pairs.