sasoptpy.actions.if_condition

if_condition(logic_expression, if_statement, else_statement=None)[source]

Creates an if-else block

Parameters
logic_expressionsasoptpy.Constraint or sasoptpy.abstract.Condition

Logical condition for the True case

For the condition, it is possible to combine constraints, such as

>>> a = so.Parameter(value=5)
>>> if_condition((a < 3) | (a > 6), func1, func2)

Constraints should be combined using bitwise operators (& for and, | for or).

if_statementfunction or IfElseStatement

Python function or if-else statement to be called if the condition is True

else_statementfunction or IfElseStatement, optional

Python function or if-else statement to be called if the condition is False

Examples

Regular condition:

>>> with so.Workspace('w') as w:
>>>     x = so.Variable(name='x')
>>>     x.set_value(0.5)
>>>     def func1():
>>>         x.set_value(1)
>>>     def func2():
>>>         x.set_value(0)
>>>     if_condition(x > 1e-6, func1, func2)
>>> print(so.to_optmodel(w))
proc optmodel;
    var x;
    x = 0.5;
    if x > 1e-06 then do;
        x = 1;
    end;
    else do;
        x = 0;
    end;
quit;

Combined conditions:

>>> with so.Workspace('w') as w:
>>>     p = so.Parameter(name='p')
>>>     def case1():
>>>         p.set_value(10)
>>>     def case2():
>>>         p.set_value(20)
>>>     r = so.Parameter(name='r', value=10)
>>>     if_condition((r < 5) | (r > 10), case1, case2)
>>> print(so.to_optmodel(w))
proc optmodel;
    num p;
    num r = 10;
    if (r < 5) or (r > 10) then do;
        p = 10;
    end;
    else do;
        p = 20;
    end;
quit;