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
VM04
Level I

How to use Words function to extract only specific elements from string

Suppose I have a string of characters with common delimiter in a form of bal1_bla2_blbla3_blabla4

And I would like to create a new column that contains only "blbla3_blabla4", how can I apply words function for that?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to use Words function to extract only specific elements from string

Hi,

 

one solution in the UI would be to use Recode to achieve what you are aiming for. 

 

Right click on your column header -> Recode -> right click on the red triangle under new value -> Advanced -> extract segment

 

Jonas_Rinne_0-1739362253495.png

 

You can find the JSL code in the log and it is using the word function:

/*:

// Recode column: Column 1 2
Local( {dt, col1},
	dt = Data Table( "Test table" );
	dt << Begin Data Update;
	col1 = dt << New Column( dt:Column 1 );
	col1 << Set Name( "Column 1 2" );
	dt << Move Selected Columns( {col1}, after( dt:Column 1 ) );
	For Each Row(
		dt,
		col1[] = Word(
			[3 4],
			dt:Column 1,
			Get Whitespace Characters() ||
			Get Punctuation Characters( Exclude Chars( "'-" ) ) || "_",
			Unmatched( dt:Column 1 )
		)
	);
	dt << End Data Update;

 

Hope this helps.

Jonas

View solution in original post

2 REPLIES 2

Re: How to use Words function to extract only specific elements from string

Hi,

 

one solution in the UI would be to use Recode to achieve what you are aiming for. 

 

Right click on your column header -> Recode -> right click on the red triangle under new value -> Advanced -> extract segment

 

Jonas_Rinne_0-1739362253495.png

 

You can find the JSL code in the log and it is using the word function:

/*:

// Recode column: Column 1 2
Local( {dt, col1},
	dt = Data Table( "Test table" );
	dt << Begin Data Update;
	col1 = dt << New Column( dt:Column 1 );
	col1 << Set Name( "Column 1 2" );
	dt << Move Selected Columns( {col1}, after( dt:Column 1 ) );
	For Each Row(
		dt,
		col1[] = Word(
			[3 4],
			dt:Column 1,
			Get Whitespace Characters() ||
			Get Punctuation Characters( Exclude Chars( "'-" ) ) || "_",
			Unmatched( dt:Column 1 )
		)
	);
	dt << End Data Update;

 

Hope this helps.

Jonas

jthi
Super User

Re: How to use Words function to extract only specific elements from string

If you want two last elements, then you can use Word with negative indexing (or positive if you know the exact locations from the beginning of the string)

Names Default To Here(1);

str = "bal1_bla2_blbla3_blabla4";

res = Word([-2 -1], str, "_");

Show(res); // res = "blbla3_blabla4";
-Jarmo

Recommended Articles