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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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