cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
  • New JMP features coming to desktops everywhere this September. Sign up to learn more at jmp.com/launch.
Choose Language Hide Translation Bar
WoHNY
Level IV

JMP 18: Select All Columns to the Right of a Specified Column

Looking to select all columns to the right of the column named , Adders. The code below runs but does not visibly select the columns to the right of Adders. The number of columns to the left and right of Adders may vary.

 

<JSL> Names Default To Here(1);

dt = Data Table("Test Table");
dt << Bring Window To Front;

Show(dt << Get Name);

// Step 1: Get all column names and show them
colNames = dt << Get Column Names();
Show(colNames);

// Step 2: Find the index of the "Adders" column
targetColName = "Adders";
colIndex = Loc(colNames, targetColName);
Show(colIndex); // Should be > 0 if found

// Step 3: Try to visually select all columns to the right
If(colIndex > 0 & colIndex < N Cols(dt),

dt << Clear Column Selection;

For(i = colIndex + 1, i <= N Cols(dt), i++,
Column(dt, i) << Set Selected(1);
);

// Step 4: Show what JMP thinks is selected
selected = dt << Get Selected Columns;
Show(selected);

,
New Window("Error", Text Box("Column 'Adders' not found, or it's the last column."));
);

 

Output:

dt << Get Name = "Test Table";
colNames = {A, B, Adders, X, Y};
colIndex = [];
3 ACCEPTED SOLUTIONS

Accepted Solutions

Re: JMP 18: Select All Columns to the Right of a Specified Column

I'd make two changes here.  You need to specify "string" in your Get Column Names message.  You should then change the Loc() function to Contains().  Loc() returns a matrix, sometimes empty.  Contains() returns an integer (0 when not found).

 

Names Default To Here(1);

dt = Data Table("Test Table");
dt << Bring Window To Front;

Show(dt << Get Name);

// Step 1: Get all column names and show them
colNames = dt << Get Column Names( "string" );
Show(colNames);

// Step 2: Find the index of the "Adders" column
targetColName = "Adders";
colIndex = Contains(colNames, targetColName);
Show(colIndex); // Should be > 0 if found

// Step 3: Try to visually select all columns to the right
If(colIndex > 0 & colIndex < N Cols(dt),

dt << Clear Column Selection;

For(i = colIndex + 1, i <= N Cols(dt), i++,
Column(dt, i) << Set Selected(1);
);

// Step 4: Show what JMP thinks is selected
selected = dt << Get Selected Columns;
Show(selected);

,
New Window("Error", Text Box("Column 'Adders' not found, or it's the last column."));
);

 

//Output:

//dt << Get Name = "Test Table";
//colNames = {"A", "B", "Adders", "X", "Y"};
//colIndex = 3;
//selected = {:X, :Y};

View solution in original post

Re: JMP 18: Select All Columns to the Right of a Specified Column

I'm sure there are other ways to do this, but the following script will find the "Adders" column, then select it and all the columns to the right:

names default to here(1);
dt = current data table();

targetCol = "Adders";
targetColNum = 0;

for( i = 1, i <= n cols( dt ), i++, 
	if( column( dt,i ) << get name == targetCol, targetColNum = i )
);
dt << select columns( targetColNum::n cols(dt) )

 

-Scott

View solution in original post

txnelson
Super User

Re: JMP 18: Select All Columns to the Right of a Specified Column

I edited your initial post and moved the JSL and its output into a JSL window and a Code window respectively, by using the input icons at the top of the edit input window.

I have also modified your code to have the << get column names, return strings, and instead of using Loc(), using Contains() to return a single value rather than a matrix value.

Names Default To Here(1);

dt = Data Table("Test Table");
dt << Bring Window To Front;

Show(dt << Get Name);

// Step 1: Get all column names and show them
colNames = dt << Get Column Names(string);
Show(colNames);

// Step 2: Find the index of the "Adders" column
targetColName = "Adders";
// Loc() returns a matrix, contains returns a scaler value
colIndex = contains(colNames, targetColName );
Show( colIndex ); // Should be > 0 if found

// Step 3: Try to visually select all columns to the right
If( colIndex > 0 & colIndex < N Cols( dt ), 

	dt << Clear Column Selection;

	For( i = colIndex + 1, i <= N Cols( dt ), i++,
		Column( dt, i ) << Set Selected( 1 )
	);

// Step 4: Show what JMP thinks is selected
	selected = dt << Get Selected Columns;
	Show( selected );

,
	New Window( "Error", Text Box( "Column 'Adders' not found, or it's the last column." ) )
);
Jim

View solution in original post

7 REPLIES 7

Re: JMP 18: Select All Columns to the Right of a Specified Column

I'd make two changes here.  You need to specify "string" in your Get Column Names message.  You should then change the Loc() function to Contains().  Loc() returns a matrix, sometimes empty.  Contains() returns an integer (0 when not found).

 

Names Default To Here(1);

dt = Data Table("Test Table");
dt << Bring Window To Front;

Show(dt << Get Name);

// Step 1: Get all column names and show them
colNames = dt << Get Column Names( "string" );
Show(colNames);

// Step 2: Find the index of the "Adders" column
targetColName = "Adders";
colIndex = Contains(colNames, targetColName);
Show(colIndex); // Should be > 0 if found

// Step 3: Try to visually select all columns to the right
If(colIndex > 0 & colIndex < N Cols(dt),

dt << Clear Column Selection;

For(i = colIndex + 1, i <= N Cols(dt), i++,
Column(dt, i) << Set Selected(1);
);

// Step 4: Show what JMP thinks is selected
selected = dt << Get Selected Columns;
Show(selected);

,
New Window("Error", Text Box("Column 'Adders' not found, or it's the last column."));
);

 

//Output:

//dt << Get Name = "Test Table";
//colNames = {"A", "B", "Adders", "X", "Y"};
//colIndex = 3;
//selected = {:X, :Y};
WoHNY
Level IV

Re: JMP 18: Select All Columns to the Right of a Specified Column

Thank you for the corrections.

Re: JMP 18: Select All Columns to the Right of a Specified Column

I'm sure there are other ways to do this, but the following script will find the "Adders" column, then select it and all the columns to the right:

names default to here(1);
dt = current data table();

targetCol = "Adders";
targetColNum = 0;

for( i = 1, i <= n cols( dt ), i++, 
	if( column( dt,i ) << get name == targetCol, targetColNum = i )
);
dt << select columns( targetColNum::n cols(dt) )

 

-Scott
WoHNY
Level IV

Re: JMP 18: Select All Columns to the Right of a Specified Column

Scott: Thank you for the code which works.

txnelson
Super User

Re: JMP 18: Select All Columns to the Right of a Specified Column

I edited your initial post and moved the JSL and its output into a JSL window and a Code window respectively, by using the input icons at the top of the edit input window.

I have also modified your code to have the << get column names, return strings, and instead of using Loc(), using Contains() to return a single value rather than a matrix value.

Names Default To Here(1);

dt = Data Table("Test Table");
dt << Bring Window To Front;

Show(dt << Get Name);

// Step 1: Get all column names and show them
colNames = dt << Get Column Names(string);
Show(colNames);

// Step 2: Find the index of the "Adders" column
targetColName = "Adders";
// Loc() returns a matrix, contains returns a scaler value
colIndex = contains(colNames, targetColName );
Show( colIndex ); // Should be > 0 if found

// Step 3: Try to visually select all columns to the right
If( colIndex > 0 & colIndex < N Cols( dt ), 

	dt << Clear Column Selection;

	For( i = colIndex + 1, i <= N Cols( dt ), i++,
		Column( dt, i ) << Set Selected( 1 )
	);

// Step 4: Show what JMP thinks is selected
	selected = dt << Get Selected Columns;
	Show( selected );

,
	New Window( "Error", Text Box( "Column 'Adders' not found, or it's the last column." ) )
);
Jim
WoHNY
Level IV

Re: JMP 18: Select All Columns to the Right of a Specified Column

Jim: Thank you for the corrections.

jthi
Super User

Re: JMP 18: Select All Columns to the Right of a Specified Column

Contains + indexing using that should make this fairly simple

 

Names Default To Here(1);

dt = Data Table("Test Table");

collist = dt << Get Column Names("String");

adders_idx = Contains(collist, "Adders");
If(adders_idx == 0,
	Throw("No Adders column found");
);

dt << Select columns(collist[adders_idx + 1::N Items(collist)]);

Show(dt << Get Selected Columns);
-Jarmo

Recommended Articles