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

How do I use a variable to reference a column in formula function?

Hi, I need to create a new column by apply formula on two existing column. I want to reference column through a variable rather than direct referencing. Here is the code , but formula seems doesn't recognize Column(ColVar). Anyone could help me to fix it? Thanks in advance. 

 

yCol = Column("weight");
xCol = Column("height");
Bivariate( Y( yCol ), X( xCol) );
colVar ="$weight";
dt = currentdatatable();
dt<<new column("sum", formula(column(ColVar)+:height));
1 ACCEPTED SOLUTION

Accepted Solutions
Georg
Level VII

Re: How do I use a variable to reference a column in formula function?

Here is just another example using

eval(eval expr( expr ()))

but using the same strategy, implement the value of the variable into the formula.

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA\Big Class.jmp" );

// get column names and show results
col_lst = dt << get column names();
my_cols= col_lst[4::5];
show(col_lst, my_cols);

// implement new formula
Eval( Eval Expr( dt << New Column( "sum of ", formula( sum(Expr(my_cols )) ) ) ) );

 

Georg

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: How do I use a variable to reference a column in formula function?

Because the formula entry is executed during the running of the column, not during the creation of the column, what is presented in the formula, needs to be the complete JSL required for processing.

Here is how I handle such issues

names default to here(1);
yCol =":weight";

dt = currentdatatable();
eval(parse("dt<<new column(\!"sum\!", formula(" || yCol || "+:height));"));
Jim
Georg
Level VII

Re: How do I use a variable to reference a column in formula function?

Here is just another example using

eval(eval expr( expr ()))

but using the same strategy, implement the value of the variable into the formula.

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA\Big Class.jmp" );

// get column names and show results
col_lst = dt << get column names();
my_cols= col_lst[4::5];
show(col_lst, my_cols);

// implement new formula
Eval( Eval Expr( dt << New Column( "sum of ", formula( sum(Expr(my_cols )) ) ) ) );

 

Georg