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
d_barnett
Level IV

Select Columns Based on Words Contained in Header

I have an analysis I'd like to perform but I need to select several columns based on the words contained in the header befre I can do this, currently I do this manually but this is painfully slow and I can miss some columns if I'm not careful.

 

in the image example I would like to select the 2 columns starting 'What colors' but none of the other 4. They can be spread amongst other columns and the number of them will be different per experiment. Once selected I can then extract them and perform the analysis.

 

This needs to be done automatically without any user input involved and I can't find any jsl that does this but I'm sure it must exist, it's just that I haven't been able to find it or don't know the correct scripting term.

 

Capture.JPG

 

Regards

 

David

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Select Columns Based on Words Contained in Header

Here is a very simple way of doing what you are requesting, and it is expandable into much more complex requirements

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/semiconductor capability.jmp" );

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

dt << clear column selection;

For( i = 1, i <= N Items( colList ), i++,
	If( 
		// Put in any selection criteria you want
		Left( colList[i], 2 ) == "PN" ,
		// or it could be
		// contains( colList[i], "PN") == 1,
		Column( dt, colList[i] ) << set selected
	)
);

If( N Items( dt << get selected columns ) > 0,
	dtSelected = dt << subset( selected rows( 0 ), selected columns( 1 ) )
);
Jim

View solution in original post

6 REPLIES 6
txnelson
Super User

Re: Select Columns Based on Words Contained in Header

Here is a very simple way of doing what you are requesting, and it is expandable into much more complex requirements

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/semiconductor capability.jmp" );

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

dt << clear column selection;

For( i = 1, i <= N Items( colList ), i++,
	If( 
		// Put in any selection criteria you want
		Left( colList[i], 2 ) == "PN" ,
		// or it could be
		// contains( colList[i], "PN") == 1,
		Column( dt, colList[i] ) << set selected
	)
);

If( N Items( dt << get selected columns ) > 0,
	dtSelected = dt << subset( selected rows( 0 ), selected columns( 1 ) )
);
Jim
ian_jmp
Staff

Re: Select Columns Based on Words Contained in Header

I don't think this adds anything given the 'no user input' requirement. But I wanted to take the chance to mention that column filtering is pretty handy when working interactively (and is easy to overlook). Sometimes people use scripts because they think it's 'hard' via the UI.

 

The 'Filter Col Selector' is also scriptable:

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
New Window( "Filter Col Selector Example", fcs = Filter Col Selector( DataTable("Big Class") ));

// See what messages the 'Filter Col Selector' understands
ShowProperties(fcs);

// Try it out
Print(fcs << GetItems);
Wait(3);
fcs << NameContains("ei");
Print(fcs << getItems);
d_barnett
Level IV

Re: Select Columns Based on Words Contained in Header

thank you very much, this works perfectly

EugeneB
Level III

Re: Select Columns Based on Words Contained in Header

I am trying to use this script to select columns that contain a string within the middle of the column name.  When I put "P" or "PN" in the contains() it works, but  not when I insert "N" or "NP" or "NP3".  

 

Thanks

For( i = 1, i <= N Items( colList ), i++,
	If( 
		// Put in any selection criteria you want
		//Left( colList[i], 2 ) == "PN" ,
		// or it could be
		Contains( colList[i], "N" ) == 1
	,
		Column( dt, colList[i] ) << set selected
	)
);

 

 

txnelson
Super User

Re: Select Columns Based on Words Contained in Header

The Contains() function does not return a Boolean of 0 or 1, it returns the character location where the comparison was found.

Try using

Contains( colList[i], "N" ) > 0

 

Jim
EugeneB
Level III

Re: Select Columns Based on Words Contained in Header

Thanks!