BookmarkSubscribeRSS Feed
mkeintz
PROC Star

I greatly appreciate the syntax PROC OPTMODEL uses in declaring array and matrics - especially for facilitating cell identification based on flexible and varied index sets.  Excellent for subsetting, and visually recognising what combination of dimensions a given cell represents.  And the user does not have to worry about reording one matrix to match another, as long as they are based on the same SET dimensions.

BUT below (in the VAR statement at the bottom - note the syntax in red) is how, to the best of my knowledge, one has to generate an X'X sum of squares and cross products from a matrix X.


That's not so bad, but what if I wanted to generate, say, beta=(X'X)-1(X'Y) or something a bit more extensive, yet trivially expressible in matrix form?  There would a lot of "sum .. in". expressions and explicit subscripting requred - easy opportunities for typing erros..  How much nicer and more readable it would be to adapt some syntax from proc IML.  Something like the violet in:

   var beta{cols} init    inv(X`*X) * X`*Y;

and have it work as long as X' and Y are compatible in their declared dimensions.

If somebody is aware of such syntax in PROC OPTMODEL, please point me to it.  Otherwise, this is a request for adding some IML-like syntax to proc optmodel statements.

** Sample prog **;
data
have;

  do r=1 to 6;

    100*uniform(015981098));

    100*uniform(015986783));

    100*uniform(015984565));

    output;

  end;

proc optmodel;

  set rows init {1..6};

  set cols init {1..3};

  num X{rows,cols} ;

  read data have into rows= {c in cols}  <x[r,c]      =col('c'||c)>;

  var XpX{c1 in cols,c2 in cols} init sum{r in 1..6} X[r,c1]*X[r,c2];

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
2 REPLIES 2
RobPratt
SAS Super FREQ

You are correct that PROC OPTMODEL does not support such matrix syntax.  In particular, there is no INV() function in PROC OPTMODEL, and matrix products are expressed using sums of products, with explicit indices, as you have indicated.  There are currently no plans to support IML-like syntax, but thank you for your suggestion.

Rob Pratt

RobPratt
SAS Super FREQ

Now that PROC OPTMODEL is officially FCMP-enabled in SAS/OR 13.1, you can access the matrix functions and subroutines that are supplied in PROC FCMP.  See Base SAS(R) 9.4 Procedures Guide, Second Edition for a complete list.  For example:

proc fcmp outlib=work.myfuncs.test;
     function mydet(x[*,*]);
          call det(x, result);
          return (result);
      endsub;
      subroutine mytranspose(x[*,*],y[*,*]);
          outargs y;
          call transpose(x,y);
      endsub;
      subroutine myinv(x[*,*],y[*,*]);

          outargs y;
          call inv(x,y);
      endsub;
      subroutine mymult(x[*,*],y[*,*],z[*,*]);
          outargs z;
          call mult(x,y,z);
      endsub;
quit;
options cmplib = work.myfuncs;


proc optmodel;
     /* matrix declaration */
     num mat1 {1..3,1..3} = [0.3, -0.78, -0.82, 0.54, 1.74, 1.2, -1.3, 0.25, 1.49];
     print mat1;

     /* determinant */
     print (mydet(mat1));

     /* matrix transpose */
     num trans {1..3,1..3};
     call mytranspose(mat1, trans);
     print trans;

     /* matrix inversion */
     num inv {1..3,1..3};
     call myinv(mat1, inv);
     print inv;

     /* matrix multiplication */
     num mat1_dot_inv {1..3,1..3};
     call mymult(mat1, inv, mat1_dot_inv);
     print mat1_dot_inv;
quit;


/* maximize determinant */
proc optmodel;
     num n = 5;
     var x {1..n, 1..n} >= -1 <= 1;
     max z = mydet(x);
     solve with NLP / ms;
     print x;
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Discussion stats
  • 2 replies
  • 2135 views
  • 1 like
  • 2 in conversation