BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Edoedoedo
Pyrite | Level 9

I'm getting stuck in defining constraints and the optimization function in matrix form for proc optmodel.

Suppose these scenarios:

 

1) I have two vectors A and B, and my constraint is A<=B (meaning: element-wise). How can I write it without a for loop to cycle the on the elements?

2) I have two matrices A and B, and my constraint is A[:, k]' * B[: ,k] <= 0 for each k in 1..n (meaning: for each k in 1..n, take the k-th column of matrix A, transpose it, take the k-th column of matrix B, and do the scalar product between the two vectors; this number must be <=0). How can I write it with only one for loop to cycle over k?

 

These conditions are very easy to write e.g. in Matlab, but I find very difficult to write them in the proc optmodel.

 

How would you do that?

Any help appreciated,

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
RobPratt
SAS Super FREQ

PROC OPTMODEL uses algebraic expressions to declare such constraints:

   var A {1..n};
   var B {1..n};
   con MyCon {i in 1..n}:
      A[i] <= B[i];
   var A {1..m, 1..n};
   var B {1..m, 1..n};
   con MyCon {k in 1..n}:
      sum {i in 1..m} A[i,k] * B[i,k] <= 0;

 

You might also be interested in this post, which shows how to use PROC FCMP with PROC OPTMODEL to access some matrix routines.

View solution in original post

2 REPLIES 2
RobPratt
SAS Super FREQ

PROC OPTMODEL uses algebraic expressions to declare such constraints:

   var A {1..n};
   var B {1..n};
   con MyCon {i in 1..n}:
      A[i] <= B[i];
   var A {1..m, 1..n};
   var B {1..m, 1..n};
   con MyCon {k in 1..n}:
      sum {i in 1..m} A[i,k] * B[i,k] <= 0;

 

You might also be interested in this post, which shows how to use PROC FCMP with PROC OPTMODEL to access some matrix routines.

Edoedoedo
Pyrite | Level 9
Thanks a lot, it really helped!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Discussion stats
  • 2 replies
  • 1159 views
  • 0 likes
  • 2 in conversation