Subscribe Bookmark RSS Feed

set a formula for columns with variable name

razmah

Community Trekker

Joined:

Jul 20, 2015

I have 3 columns with variable names.

how can I add new column and give it a formula by use of a,b, and c?

I wrote following script but column doesn't show the formula.

New Column( "a /b",Numeric,Continuous,Format( "Best", 12 ),

Formula( :column(char(a)) /  :column( char(b)) ));

thank you for your recommendation

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

Ah OK.  Here's a better example:

a = 10;

b = 20;

dt = New Table( "Example", Add Rows( 3 ),

     New Column( char(a), Numeric, Continuous, Format( "Best", 12 ),

           Set Values( [1, 2, 3] ) ),

     New Column( char(b), Numeric, Continuous, Format( "Best", 12 ),

           Set Values( [4, 5, 6] ) ),

);

new_expr = evalinsert(

"\[dt << New Column( "a/b", Numeric, Continuous, Format( "Best", 12 ),

     Formula( :Name( "^a^" ) / :Name( "^b^" )))]\");

eval(parse(new_expr));

6 REPLIES
pmroz

Super User

Joined:

Jun 23, 2011

This will work:

a = "weight";

b = "height";

dt = open("$sample_data\Big Class.jmp");

new_expr = evalinsert(

"\[dt << New Column( "a/b",Numeric,Continuous,Format( "Best", 12 ),

Formula( :^a^ / :^b^ ))]\");

eval(parse(new_expr));

razmah

Community Trekker

Joined:

Jul 20, 2015

My "a" and "b" are not name; thay are numbers.

do I need any changes for that? because I tried your script, it didn't work for me.

Solution

Ah OK.  Here's a better example:

a = 10;

b = 20;

dt = New Table( "Example", Add Rows( 3 ),

     New Column( char(a), Numeric, Continuous, Format( "Best", 12 ),

           Set Values( [1, 2, 3] ) ),

     New Column( char(b), Numeric, Continuous, Format( "Best", 12 ),

           Set Values( [4, 5, 6] ) ),

);

new_expr = evalinsert(

"\[dt << New Column( "a/b", Numeric, Continuous, Format( "Best", 12 ),

     Formula( :Name( "^a^" ) / :Name( "^b^" )))]\");

eval(parse(new_expr));

razmah

Community Trekker

Joined:

Jul 20, 2015

GREAT! Thanks

pmroz

Super User

Joined:

Jun 23, 2011

For those not familiar with evalinsert or \[...]\:

evalinsert allows the insertion of variables surrounded by the caret character "^".  Makes it easier to build a string containing variables by avoiding lots of concatenating.

The special characters \[ in a string tell JMP not to interpret anything in the string, until you see the characters ]\.  Allows for clean insertion of double quotes, among other things.

gutloja

Community Trekker

Joined:

Jul 31, 2015

Hello, Razmah.

If all you want is to insert a new column to an already open file, with a formula using "A", "B, and "C" columns [from your data table], just run a similar script on your Script Editor:

    New Column( "A/B", Numeric, Continuous, Format( "Best", 5 ), Formula( a / b ) );

    New Column( "B/C", Numeric, Continuous, Format( "Best", 5 ), Formula( b / c ) );

    New Column( "C*D", Numeric, Continuous, Format( "Best", 5 ), Formula( c * d ) );

    New Column( "A+C+B", Numeric, Continuous,Format( "Best", 5 ),Formula( a + b + c));

Your script is missing the "Formula ( ) " function.

-Jose