Statistical programming, matrix languages, and more

Referencing global matrix inside function in proc iml

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Referencing global matrix inside function in proc iml

Hi!

 

I am new to proc iml (and SAS in general) and am attempting to use proc iml optimization. Unfortunately, I am having trouble referencing a matrix that I created. The idea of my code is below:

 

proc iml;

use mysasdataset;

read all var into VARS;

close mysasdataset;

concent = VARS[,1];

print concent;

show names;

start F_HRCOPT(x);

     f=concent*matrix  (some other stuff follows here; the matrix to which I refer is 1x1).

     return f;

finish F_HRCOPT;

...continue with code...

 

The issue that I'm having is that when I use "show names;" it does state that the variable concent has been created and that its dimensions are Nx1 (which is exactly what I want; picture attached).  However, when I look at my SAS log it indicates that concent is empty (0x0). Any ideas why? Do I have to reference the matrix in a special way inside of the function? My thoughts are that the matrix "concent" should be a global matrix since it's defined outside of the function, which leads me to believe that I should be able to reference it inside of the function. Any insight would be greatly appreciated.

 

Thank you!!

 

JMM

Attachment

Accepted Solutions
Solution
‎12-07-2016 12:48 PM
SAS Super FREQ
Posts: 3,232

Re: Referencing global matrix inside function in proc iml

The problem is that SAS/IML functions use a local symbol table, so although the CONCENT matrix is known in the main (parent) environment, that symbol is not known to the function.  For a more comprehensize explanation, see the article "Understanding local and global variables in the SAS/IML language."

 

In short, you must pass in matrices to function via the argument list of declare them to be a global variable by using the GLOBAL clause.

 

If you intend to do some optimization in SAS/IML, I recommend the following articles:

 

You might also want to keep this documentation handy:

 

View solution in original post


All Replies
Solution
‎12-07-2016 12:48 PM
SAS Super FREQ
Posts: 3,232

Re: Referencing global matrix inside function in proc iml

The problem is that SAS/IML functions use a local symbol table, so although the CONCENT matrix is known in the main (parent) environment, that symbol is not known to the function.  For a more comprehensize explanation, see the article "Understanding local and global variables in the SAS/IML language."

 

In short, you must pass in matrices to function via the argument list of declare them to be a global variable by using the GLOBAL clause.

 

If you intend to do some optimization in SAS/IML, I recommend the following articles:

 

You might also want to keep this documentation handy:

 

Occasional Contributor
Posts: 7

Re: Referencing global matrix inside function in proc iml

I thought that might be the case! Thank you! Smiley Happy

Post a Question
Discussion Stats
  • 2 replies
  • 161 views
  • 0 likes
  • 2 in conversation