Statistical programming, matrix languages, and more

User-defined module with multiple outputs?

Reply
Contributor
Posts: 37

User-defined module with multiple outputs?

Hi, there:

I am wondering if I can define some modules with mutliple outputs in IML. For example, I want to write a simple module to do linear regression and output both the estimated coefficients and residual series. Can I write it in this way?

start ols (y,x);

beta=inv(x`*x)*(x`*y);

residual=y-x*beta;

return (beta,residual);

finish ols.

When I want to use it, can I write a line in the main body of code like the following:

...

{beta,residual}=ols(y_data,x_data).

...

I don't find any sample online. Some subroutines following the "call" keyword can give multiple outputs, however, the output names are prefixed. I want to be able to control the output names.

Thanks.

SAS Super FREQ
Posts: 3,898

User-defined module with multiple outputs?

Posted in reply to bigbigben

You can't list multiple matrices on the RETURN statement (that's MATLAB syntax) but you can return as many values as you want by writing a subroutine module instead of a function module.

For an example, see the bottom of p. 2 of http://support.sas.com/resources/papers/proceedings10/329-2010.pdf

Incidentally, you can do this because SAS/IML passes arguments by reference, not by value.

The SAS/IML convention is that output arguments are listed first, so your module might look like

proc iml;

start ols (beta, residual, y,x);

  beta=inv(x`*x)*(x`*y);

  residual=y-x*beta;

finish ols;

/* define data */

x= { 1 1 1,     1 2 4,     1 3 9,     1 4 16,     1 5 25,     1 6 36,     1 7 49,     1 8 64 };

y= {3.929,5.308,7.239,9.638,12.866,17.069,23.191,31.443};

/* call module */

run ols(b, r, y, x);

print b, r;

Ask a Question
Discussion stats
  • 1 reply
  • 229 views
  • 0 likes
  • 2 in conversation