BookmarkSubscribeRSS Feed
bigbigben
Obsidian | Level 7

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.

1 REPLY 1
Rick_SAS
SAS Super FREQ

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;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 1 reply
  • 1435 views
  • 0 likes
  • 2 in conversation