- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
My problem is how to save optimal value of the objective function from one optmodel procedure into the global variable. I need this in order to use it in the second optmodel procedure. I've prepared a simple (dummy) example code which illustrates my problem.
%LET OptimalValue=0;
%macro outer;
%firstmodel(5);
*other code;
%put "opt in outer =" &OptimalValue;
%mend outer;
%macro firstmodel(i);
proc optmodel;
num y;
var x;
max f=x;
con c1: x<=&i;
solve;
y=f;
put y;
%let OptimalValue = y; *here string "y" is assigned to OptimalValue, not the value of y;
%put "opt in first model=" &OptimalValue;
quit;
%mend firstmodel;
%outer;
How to access a value of local variable and assign it to global variable inside proc optmodel?
In fifth line from the bottom of code SAS understands y as string and not as a variable.
I would appreciate any help. Thanks
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks PGStats for your answer,
In _OROPTMODEL_ variable there is also optimal value (among other), but I have an optimal value also in my local variable y.
Your hint about looking at _OROPTMODEL_ documentation lead me to "SYMGET" and "CALL SYMPUT". The last one turned out to be a needed element.
Below, is a corrected version of my code - just for someone who would have similar problem with assignment of local variable to global variable.
%LET OptimalValue=0;
%macro outer;
%firstmodel(8);
*other code;
%put "opt in outer =" &OptimalValue;
%mend outer;
%macro firstmodel(i);
proc optmodel;
num y;
var x;
max f=x;
con c1: x<=&i;
solve;
y=f;
put y;
call symput('OptimalValue',trim(left(put(y,8.))));
%put "opt in first model=" &OptimalValue;
quit;
%mend firstmodel;
%outer;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Look into the _OROPTMODEL_ macro variable for the substring OBJECTIVE=value. To quote the documentation:
The OPTMODEL procedure creates a macro variable named _OROPTMODEL_. You can inspect the execution of the most recently invoked solver from the value of the macro variable. The macro variable is defined at the start of the procedure and updated after each SOLVE statement is executed. The OPTMODEL procedure also updates the macro variable when an error is detected. The _OROPTMODEL_ value is a string that consists of several "KEYWORD=value" items in sequence, separated by blanks; for example: STATUS=OK ALGORITHM=DS SOLUTION_STATUS=OPTIMAL OBJECTIVE=119302.04331 PRIMAL_INFEASIBILITY=3.552714E-13 DUAL_INFEASIBILITY=2.273737E-13 BOUND_INFEASIBILITY=0 ITERATIONS=82 PRESOLVE_TIME=0.02 SOLUTION_TIME=0.05 The information contained in _OROPTMODEL_ varies according to which solver was last called. For lists of keywords and possible values, see the individual solver chapters.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks PGStats for your answer,
In _OROPTMODEL_ variable there is also optimal value (among other), but I have an optimal value also in my local variable y.
Your hint about looking at _OROPTMODEL_ documentation lead me to "SYMGET" and "CALL SYMPUT". The last one turned out to be a needed element.
Below, is a corrected version of my code - just for someone who would have similar problem with assignment of local variable to global variable.
%LET OptimalValue=0;
%macro outer;
%firstmodel(8);
*other code;
%put "opt in outer =" &OptimalValue;
%mend outer;
%macro firstmodel(i);
proc optmodel;
num y;
var x;
max f=x;
con c1: x<=&i;
solve;
y=f;
put y;
call symput('OptimalValue',trim(left(put(y,8.))));
%put "opt in first model=" &OptimalValue;
quit;
%mend firstmodel;
%outer;