cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
qt
qt
Level I

How to use column name as a variable.

I want to select some rows from a column ( I have a column named as 'RESOLUTION' in the current data table)

This two lines will work:

======================================

dt = Current Data Table();

dt<<Select Where(RESOLUTION > 0.);

======================================

If I define 'RESOLUTION' as another variable, it doesn't work:

============================================

dt = Current Data Table();

colA="RESOLUTION";

dt<<Select Where(column(dt, colA) > 0.);

============================================

This will not select any row.

Could someone kindly tell me how could I do this?

Thanks.

5 REPLIES 5
pmroz
Super User

Re: How to use column name as a variable.

qt,

This will do the trick.

dt = Current Data Table();

colA = "Resolution";

col_expr = "dt << Select Where(:name(\!"" || colA || "\!") > 0.)";

eval(parse(col_expr));

Basically you're creating some dynamic code, and then executing it.

qt
qt
Level I

Re: How to use column name as a variable.

It works!!

Many thanks!

David_Burnham
Super User (Alumni)

Re: How to use column name as a variable.

Alternatively use the AsColumn function:

colA="RESOLUTION";

dt<<Select Where(AsColumn(colA) > 0.)

-Dave
tsl
tsl
Level III

Re: How to use column name as a variable.

One thing to watch with using As Column() is that it seems to be much slower than using the eval( parse(..)) syntax for a large data table.

In my case I had a data table with 390k rows and 17 columns.

Using Eval ( Parse ( .... )) took 67 msec to select the rows I was looking for while using the As Column() syntax took 3,600 msec !  ( using HPTime() to measure )

So while the As Column() syntax is a whole lot more readable in the code, if speed matters, it may not be the best option

jthi
Super User

Re: How to use column name as a variable.

It might make AsColumn() faster if you combine it with Column().

Re: Should you Loop through a data table or use Recode, or use Get Rows Where to change values in a ... 

 

 

-Jarmo