BookmarkSubscribeRSS Feed
Calcite | Level 5


I have a decision variable (x) that should be outputted from an optimization problem

This variable should be outputted as binary, taking only the values of "0" OR "1"

How can I define that at SAS proc iml?

As I can't find a syntax that puts that as a condition

In addition, I tried the below equation to be added to the constraints of my optimization problem, which ends up with the same meaning, but it gave no results


Please help

this is very urgent and I have due date with very limited timingssssssssssss

thanks a lot


In the SAS DATA step and in PROC IML you can form a logical expression that evaluates to true (1) or false (0).
For example, if you want to return 1 when x>0.7, just write

result = ( x>0.7 ); /* 0 or 1 */

Calcite | Level 5

sorry cant get what you mean quite well,

here are the below equations that I need to write at sas proc iml:


where b is a decision variable representing my coefficients at the model

c is a binary decision variable that should be outputted as 0 or 1, so how can I express that at my iml program?


Are you using the NLPQN function? If so, I think you need to specify the nonlinear constraint by using the NLC= option. See

the doc chapter "Nonlinear Optimization Examples: Parameter Constraints"

Calcite | Level 5

unfortunately, that doesn't solve my problem:

the below is my model I need to write at sas proc iml:



sum c(i)=4;

given that i=1.........6

these equations shows that: if c (it is outputted) =1, then 0.1<b<1

and if c=0, then b=0

This means that I need 4 c's =1 and 2 c's =0

and also I need 2 b's=0 and 4 b's having values ranging from 0.1 to 1

where b is a decision variable representing my coefficients at the model

c is a binary decision variable that should be outputted as 0 or 1, so how can I express that at my iml program?

did you get what I am trying to output?

SAS Employee

One quick and dirty way to handle this would be to enumerate the 15 different possibilites for c and optimize b individually for each sub-problem.  Are you trying to solve just this specific problem or is it an example of a more general problem you are trying to solve?

Calcite | Level 5

Wow, that would be too difficult as that is just an example of equations at many problems I am trying to handle Smiley Sad

are there any other ways of solving that problem? I have been trying to solve that problem for several monthsssssssssssss


SAS Employee

what is your objective function?

SAS Employee

Depending on the problem size and your objective function, there are different ways to solve the problem you describe. If your objective function is linear, then you have a Mixed Integer Linear Program. If you have SAS/OR available to you, then you can use PROC OPTMILP to solve that directly.

If your objective function is a general non-linear function, then you have a Mixed Integer Non-linear Programming problem (MINLP) which in general is difficult to solve. If that is the case then in IML your best bet would be to use the Genetic Algorithm routines to try and solve your problem.

If you are trying to fit a traditional statistical model  like a linear or logistic regression, then SAS/STAT offers extensive variable selection algorithms you can use to find the optimal set of decision variables.

Perhaps if you would describe the original problem that would help in figuring out the best way to solve it..


Try using the augmented Lagrangian method. The method gets rid of the nonlinear constraint by rewriting the objective function to include a penalty term.  By adding a "nuisance parameterm," you can solve the original problem.  For example, you might try introducing a new parameter lambda>0 and optimizing the objective function

OrigObjective + lambda*x*(1-x) + 10000*(x*(1-x))##2

In order to get rid of the penalty terms, x will be driven to 0 or 1 during the optimization.

Calcite | Level 5

Here is my model:

Minimize dp+dn

subject to



sum ci=4

where i=1..........6

j is my set of observations

and my decision variables are:

b, dp, dn, and c

hope that makes my problem more clear for you to please assisttttttttttttttttt

Calcite | Level 5

I need also to make a simulation for that problem?

and also sometimes I have non-linear constraints at another problem I am doing simulation for and using at that stage nlpqn at sas proc iml

SAS Employee

This appears to be a MILP problem. The best SAS tool to solve this would be PROC OPTMILP in SAS/OR. Do you have that

Calcite | Level 5

unfortunately no Smiley Sad

is there any other way using sas proc iml please?

I am using nlpqn call

SAS Employee

Here is what I recommend, going back to my earlier suggestion.

1) generate all possible c vectors

2) for each c vector, solve the optimization problem treating the c as a constant.

this gives you a simple optimization problem with linear constraints.

3) the c vector that gives you the best optimum value coresponds to your global optimum value.

Here's how to get started:

Create a module that solves the problem for a given c vector, treating c as a constant, like

start optimize(opt_value, opt_vec) global( c);

/* set up constraints */


   call nlpqn(rc, opt_vec, "objective_function"....); /* do optimization  for a given c vector*/

   opt_value = objective_function(opt_vec);


then loop through all the possible values of c:

ncoeff = 6; /* total number of c variables */

subset_size = 4; /* number of c variables to set to 1 */

perms = allcomb(ncoeff,subset_size); /* get all permutations of 4 out of 6 elements */

optval = 1e30; /* initialize to some some huge value */

do i = 1 to nrow(perms);

  /* set up c vector */

  c = j(ncoeff, 1, 0);

  c[  perms[i,],   1  ] = 1;

  run optimize(value, vector);

  if value < optval then do;

    /* save optimal values */

    optval = value;

    optvec = vector;

    optc = c;



now you should have your optimum decision variables in optvec and optc and your optimum value in optval



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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
  • 16 replies
  • 3 in conversation