Statistical programming, matrix languages, and more

variables in nested modules

Reply
Contributor
Posts: 22

variables in nested modules

I run the code to optimizing loglikelihood  like the following in SAS9.2/IML:

start ML (x) ;

dp=1;x1=2;,x2=3;

star ls(a) global(dp, x1,x2);

z=SSQ(dp-a[1]#x1-a[2]#x2);

return(z);

finish LS;

x={3 3};

call nlpnrr(rc,rx,"LS",x);

....

finish ML;

x={0,1};

call nlpnms(rc,xr,"ML",x);

there is a error message in log window as following:

ERROR: (execution) Matrix has not been set to a value.

operation : # at line 1803 column 30
operands  : _TEM1001, x1

_TEM1001      1 row       1 col     (numeric)

         3

x1      0 row       0 col     (type ?, size 0)


statement : ASSIGN at line 1803 column 17
traceback : module LS at line 1803 column 17
             module ML at line 1635 column 1

ERROR: (execution) Matrix has not been set to a value.

operation : NLPNRR at line 1686 column 16
operands  : *LIT1074, x, , con

*LIT1074      1 row       1 col     (character, size 2)

ls

x      1 row       2 cols    (numeric)

         3         3

con      2 rows      2 cols    (numeric)

         .         .
         .        50

statement : CALL at line 1686 column 16
traceback : module ML at line 1686 column 16

When  i use  a Global clause in Start ML(x) as

Start ML(x) Global(dp, x1,x2);

there is not error message.

My question is :

Since x1 and x2 are defined with global clause in moldule ML before model LS, they are global variables for module LS, Why error message say that? What's the function of the Global clause in "Start ML(x) Global

(dp, x1,x2);"

Thanks alot!

SAS Super FREQ
Posts: 3,234

Re: variables in nested modules

There is no such thing as a nested module. All modules are known globally. If you want to share variable names between two modules, you can use the GLOBAL statement on each START statement. In your case, you would use

global(dp, x1,x2);

for both the ML and LS modules.

For details, see the article Local functions (not!) in the SAS/IML language - The DO Loop

SAS Super FREQ
Posts: 3,234

Re: variables in nested modules

By the way, your LS function doesn't have a unique minimum. It has a trough, which will probably result in the optimization step not converging. Notice that the gradient of LS is

start grdLS(a) global(dp, x1,x2);

  return( -x1 || -x2 );

finish;

The gradient is a constant vector, so it never vanishes unless x1 and x2 are identically zero.

Post a Question
Discussion Stats
  • 2 replies
  • 268 views
  • 6 likes
  • 2 in conversation