You can get it to work, with
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );
columnname1 = Column( dt, 4 ) << Get Name;
columnname2 = Column( dt, 5 ) << Get Name;
dt << New Column( "new_" || columnname1,
Numeric,
continuous,
formula(
As Column( dt, columnname1 ) + As Column( dt, columnname2 )
)
);
However, there is a potential problem with this. The stored formula is:
This formula will continue to work, as long as your JMP session
- Has the variables, dt, columnname1 and columnname2 defined. What happens if you save the data table, shutdown JMP, and then tomorrow you come in and startup JMP, and open the data table. The formula will not find those variables, and the formula will fail.
- Within the session, the variables, dt, columnname1 and columnname2, do not change their value. So, if you create this column, and later on in your script, you happen to change the value of variable columnname1, since formula columns in a JMP data table are dynamic, the formula will rerun when changes to the data table take place, JMP will use the new value of the variable, and will create new values for the formula column.
What you really need to do, is to use a method that creates the actual JSL syntax for the formula. In the above example, what you really want is for the formula to be
:height + :weight
The JSL below is one way to do this. It uses the Substitute() function to implant the actual column names into the formula.
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );
columnname1 = Column( dt, 4 ) << Get Name;
columnname2 = Column( dt, 5 ) << Get Name;
Eval(
Substitute(
Expr(
dt << New Column( "new_" || columnname1,
Numeric,
continuous,
formula( __c1__ + __c2__ )
)
),
Expr( __c1__ ), Parse( ":" || columnname1 ),
Expr( __c2__ ), Parse( ":" || columnname2 )
)
);
Jim