cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
See how to use to use Text Explorer to glean valuable information from text data at April 25 webinar.
Choose Language Hide Translation Bar
View Original Published Thread

JSL: Creating formula using a variable column name

Hi,

I want to do an actually simple thing (at least I thought so), namely to create a new column using JSL with a formula that contains a variable column name. I already tried several combinations of the commands "column, as column, name, as name, eval, parse .." and I searched the forum, but everything I tried results in an error that the formula could not be evaluated.

An example what I want to do:
"column 1" is a column in my data table.

a = "column 1";
new column("test", formula(a*2));

The column "test" should contain the formula that multiplies "column 1" with 2.

Thanks for any help,
Dahla

13 REPLIES 13

Re: JSL: Creating formula using a variable column name

@mpb: That works fine! Thanks for finding out and for the book tip!

Re: JSL: Creating formula using a variable column name

I tried but cant get it work on a variant.

There are two column where the user chooses in a custom dialog box, and i assign them the names of lx,ly.
I need a new column with formula

Radius = SQRT((X^2)+(Y^2))

However, when i run it, althought the new column is created, JMP prompt me
"N Items() argument must be a list", and no formula created.

Appreciate if a kind soul can advice on which part is faulty. Thanks.

customDlg << CloseWindow;
lp = colListP << GetItems;
lx = colListX << GetItems;
ly = colListY << GetItems;
 
// Are the user selections viable?
if (NItems(lp) < 1,
     Dialog("ERROR: Select one or more Parameter Columns", Button("OK")); Throw());
if (NItems(lx) != 1,
     Dialog("ERROR: You need to select an X Column", Button("OK")); Throw(),
     xVar = lx(1)     
     );
if (NItems(ly) != 1,
     Dialog("ERROR: You need to select a Y Column", Button("OK")); Throw(),
     yVar = ly(1)
     );
 
 
// *********************************************************************************
// Build New Column with new formula
// *********************************************************************************
 
dt = Current Data Table();
a = Column(dt,xvar);
b = Column(dt,yvar);
colx = ;
coly = ;
nlist = N Items(a);
For( i = 1, i <= nlist, i++,     
newColExpr = Expr(          
               dt << New Column( "Radius", Numeric,
               Formula( Root((Power(Expr(a(i)),2)+Power(Expr(b(i)),2)),2 ) )     );     
               Eval( Eval Expr( newColExpr ) );
);
);
); 
mpb
mpb
Level VII

Re: JSL: Creating formula using a variable column name

Here's an example using the Column Dialog which gives the user the choice to select exactly 2 columns from which the Radius column will be created.

dt = Current Data Table();
cdlg = Column Dialog( clist = ColList( "Pick 2 Columns", Min Col( 2 ), Max Col( 2 ) ) );
cols = cdlg["clist"];
 
newColExpr = Expr( dt << New Column( "Radius", Numeric, Formula( Sqrt( Expr( cols&#91;1&#93; ) ^ 2 + Expr( cols&#91;2&#93; ) ^ 2 ) ) ) );
 
Eval( Eval Expr( newColExpr ) );

Re: JSL: Creating formula using a variable column name

Thanks for the help. Will try it out.
=)