cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
mikedriscoll
Level VI

How to get column number from column name? (JMP)

This may be a really simple question but after a lot of searching and testing I can't figure it out.  How do I get the column number for a given column if all i know is the column name from a column dialog box?

Minimized code:

dlg = Column Dialog( dlg_firstParam = ColList( "First Parameter"));

firstParam = dlg["dlg_firstParam"][1];

i = 5; // this is a test case.  Assume i am going to select the fifth column in the dialog above.  If this works, I would write a loop for i = 1 to ncol() and test Column Name ( i ) against the first parameter name... but it doesn't work.

show(column(i)); // returns:   Column("Date");

show(firstParam); // returns:   :Date;

show(columnName(i)); //returns:   Date;

//Ok, so given the above two lines, i think I should be able to strip out the colon at the beginning of the string in :Date; and compare to Date; as below.  Almost looks like it will work...

a = parse(right(expr(firstParam),length(expr(firstParam))));

show (a); // Returns:  Date;

// Ok, so the colon gets stripped out.

if(columnName(i)==a,b = 1, columnName(i)!=a, b = 0);

show (b); // returns 0

//////////////////

The above code is a test to see if i can loop through and test if column name ( i ) = a function of firstParam. Looks like it should work, but doesn't. Any ideas?

The preferred method would be to just return the column number directly.  If you can help me with that I would appreciate it.

(Edit: Just a note to change the last variable in your code from colnumber to some non-JMP keyword. colnumber is reserved. Otherwise works fine.)

1 ACCEPTED SOLUTION

Accepted Solutions
ms
Super User (Alumni) ms
Super User (Alumni)

Re: How to get column number from column name? (JMP)

You can use the Loc() function to locate the position of a column name in a list of all column names (ordered as in the data table).

Example:

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

collist = dt << get column names( string );

colname = Column( "height" ) << getname;

colnumber = (Loc( collist, colname ))[1];

View solution in original post

3 REPLIES 3
ms
Super User (Alumni) ms
Super User (Alumni)

Re: How to get column number from column name? (JMP)

You can use the Loc() function to locate the position of a column name in a list of all column names (ordered as in the data table).

Example:

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

collist = dt << get column names( string );

colname = Column( "height" ) << getname;

colnumber = (Loc( collist, colname ))[1];

mikedriscoll
Level VI

Re: How to get column number from column name? (JMP)

Thanks MS.  Just a note to change the "colnumber" in the last line of your code to some non-JMP keyword. Otherwise it works fine.

I guy I work with suggested a similar solution by obtaining the number directly:

collist = dt << get column names( string );

firstParam_name = firstParam << get name;

ColumnNumber =contains(collist, firstParam_name);

twillkickers
Level III

Re: How to get column number from column name? (JMP)

Thanks for the solution! A little tip to those out there, if the datatable is not active on the line

colname = Column( "height" ) << getname;

Then this line will not work. However, if you specify both the datatable and column name:

colname = dt:height << getname;

It works perfectly! Just offering some advice so others dont get hung up like I did ;)