Proc Optmodel inside Macro calling FCMP function?

Occasional Learner
Posts: 1

Proc Optmodel inside Macro calling FCMP function?

Question/ Help with symbolic interpretation of Proc Optmodel optimization variable not being a number when being passed to FCMP function from within a Macro.

I have a data set that I want to loop through (10+ groups) and optimize a solution by using the min f = myfunc(x1, x2).

My dataset comprises of three variables, character, number, number (grouping, var1 and var2).

Example Code:

%Macro mymacro (dataset);          /* Macro set up to loop over the group variable and solve using custom function */

Proc SQL NoPrint;                         /* defining the distinct group to loop over */

Select Distinct Group

     into :Grouping Sperated by ","

From &Dataset;


%let i = 1;

%Do %While (%Scan(%bquote(&Grouping), &i, %Str(,)) ne);

%let Group = "%Scan(%bquote(&Grouping), &i, %Str(,))";

Proc SQL;                                             /* subsetting the original dataset down to the various groups, only need to pass the var1 and var2 to the function */

Create Table work.subset AS

Select var1,


From &Dataset

Where grouping = &Grouping;


Proc SQL Noprint;

Select count(var1) into :arraySize

From work..subset;


Proc fcmp outlib=work.myfuncs.test;                    /* setup myfunction (this is not the actual function but used to show what I am trying to do) */

     function test(x1, x2);

     array var1[&arraySize.] /nosymbols;

     array var2[&arraySize.] /nosymbols;

     rc = read_array(work.subset, var1, 'var1');

     rc = read_array(work.subset, var2, 'var2');

do i = 1 to &arraySize;

     calc = calc + var1*2 + var2*3;


return (calc);




options cmplib = work.myfuncs;

proc optmodel;

     var x1 >= 0.5 <= 8 init 1,

          x2 >= 1 <= 100 init 50;

     min f = %sysfunc(test(x1, x2));

     Solve with NLPC;

     print f x1 x2;


%let i = %eval(&i+1);


%mend mymacro;



When I run the example of this code the macro will loop through the different groups of data, the function is created and compiled and the proc optmodel calls the function.  The error that I get is "argument to function test referenced by %sysfunc or %Qsysfunc macro function is not a number".  I do not know how to resolve the symbolic representation of the optimization variable to a number and pass it to the function.

If I hard code the inputs to the function (min f = %sysfunc(test(1, 5))Smiley Wink everything works fine so I know it is not the macro, function or the optmodel structure but how to get the optimization variable to be represented as a number before passing to the function.

Is there a way to represent the optimization variable as a number and not a symbolic local variable within the optmodel structure?

Any help would be great...

SAS Employee
Posts: 526

Re: Proc Optmodel inside Macro calling FCMP function?

Questions about PROC OPTMODEL in SAS/OR are better suited for the Mathematical Optimization and Operations Research Community.  If you still need help, please post there.

Super User
Posts: 12,640

Re: Proc Optmodel inside Macro calling FCMP function?

I think you are seeing a result from using a macro function where not needed. Please run these two pieces of code and see if you get similar behavior from them.


data _null_;
   x=3; y=5;
   j = %sysfunc(min(x,y));
   put j=;

data _null_;
   x=3; y=5;
   j = %sysfunc(min(3,5));
   put j=;

The specific message looks like you may be using a call to %SYSFUNC when inappropriate. %SYSFUNC being a macro function is not able to see the data step variables x and y in my example. 


Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation