cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
thedellette
Level II

Create new column with formula saved as a variable in a script

I am using a script to extract a predicted formula from some data that I have, I then modify that formula using substitution and would now like to make a new column using the modified formula. I think this is easy but when I try to script this the formula saved as a variable is not recognized. Any help would be greatly appreciated, please let me know if you need more information as well.

//Script that just shows saving the formula as a variable fm and modifying the variable using substitute to fm2

fm = report(obj1)[OutlineBox("Polynomial ?")][TextEditBox(1)] << get text;
//get the string to the right of the equal sign and convert the string to an expression using Parse()
fm = Parse(Trim(word(2, fm, "=")));
fm2 = Substitute(NameExpr(fm), EvalExpr(LnTime2), EvalExpr(log(Time2)));

 

//Script to create a new column with the fm2 formula that does not work

dt << New Column( "Predicted", Numeric, Formula (fm2));

//Here is what is returned in the Log when you print fm2 in the script

Print(fm2);

(-0.270568) + 0.0352458 * Log(Time2) - 0.0331127 * (Log(Time2) - 7.22484) ^ 2 + 0.0046766 * (Log(Time2) - 7.22484) ^ 3 + 0.0075752 * (Log(Time2) - 7.22484) ^ 4 + 0.0012501 * (Log(Time2) - 7.22484) ^ 5 + 0.000026534 * (Log(Time2) - 7.22484) ^ 6

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Create new column with formula saved as a variable in a script

Try this instead:

 

Eval(
	Substitute(
		Expr( dt << New Column( "Predicted", Numeric, Formula ( fff ) ) ),
		Expr( fff ), Name Expr( fm2 )
	)
);

View solution in original post

4 REPLIES 4

Re: Create new column with formula saved as a variable in a script

Try this instead:

 

Eval(
	Substitute(
		Expr( dt << New Column( "Predicted", Numeric, Formula ( fff ) ) ),
		Expr( fff ), Name Expr( fm2 )
	)
);
thedellette
Level II

Re: Create new column with formula saved as a variable in a script

Thank you!

txnelson
Super User

Re: Create new column with formula saved as a variable in a script

I would keep the formula as a string, substitute within it, adding a ":" to let know JMP you are dealing with a column and then use

     eval(parse())

to execute it.  Here is a simple exampel

names default to here(1);
dt=current data table();

fm="height";
fm2 = substitute(fm, expr("height"), "log(:height)");

eval(parse("dt << new column (\!"predicted\!",numeric,formula(" || fm2 || "));"));
Jim
thedellette
Level II

Re: Create new column with formula saved as a variable in a script

Thanks for the tip to keep the formula as a string and adding  ":" to let JMP know if is a column. I couldn't get the eval(parse()) to work, I was unsure why the double quote was needed in this as well.