Is there a specific reason to avoid doing what you are currently? If column reference is empty, don't add it to the formula.
There really isn't anything complex in your way of doing it and it is easy to read and understand. You could use if-statement to just set the formula which will reduce the repetition slightly. I would use either this or the way you are doing it (if I understand your question correctly)
Names Default To Here(1);
// myvar = 1;
dt = Open("$SAMPLE_DATA/Big Class.jmp");
newcol = dt << New Column("Test", Numeric, Continuous);
If(Is Missing(myvar),
newcol << Set Formula(
:height/:weight
);
,
newcol << Set Formula(
:weight/:height
)
);
Some more difficult to read, understand and possibly debug options:
You can build the formula expression in if statement and then set the formula
Names Default To Here(1);
myvar = 1;
dt = Open("$SAMPLE_DATA/Big Class.jmp");
fexpr = If(Is Missing(myvar), Name Expr(:height/:weight), Name Expr(:weight/:height));
Eval(EvalExpr(
newcol = dt << New Column("Test", Numeric, Continuous, Formula(
Expr(NameExpr(fexpr))
))
));
Or you can use the if statement inside your formula evaluation (unnecessary complicated in my opinion)
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
newcol = dt << New Column("Test", Numeric, Continuous);
Eval(Substitute(
Expr(newcol << Set Formula(
_fexpr_
)),
Expr(_fexpr_), If(Is Missing(myvar), Name Expr(:height/:weight), Name Expr(:weight/:height))
));
-Jarmo