Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Highlighted
DanAlexander
Level II

Removing the end of a string of variable length

Hello all,

 

I have a column :Protocol which contains strings of variable length in the format Character, Nominal. I've added some examples below. I am trying to remove the last 4 characters of the string from the column, and keep everything else. The last 4 characters are always one of: -001, -002, -003, or -004. I have tried to accomplish this using Word(), and Substr() but am unable to get it to work:

 

Protocol

AAA-BB-1234-19-001

AAAA-B-18-002

AA-BBB-123-18-003

AA-B-CC-1001-19-002

 

This is what I want the column to look like for those 4 rows:

 

Protocol

AAA-BB-1234-19

AAAA-B-18

AA-BBB-123-18

AA-B-CC-1001-19

 

I have tried to accomplish this using the Word() functions as follows:

For Each Row(
	:Protocol = Word(1, :Protocol, "00")
);
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
David_Burnham
Super User

Re: Removing the end of a string of variable length

Something like Left(:Protocol, Length(:Protocol) - 4)
-Dave

View solution in original post

4 REPLIES 4
Highlighted
David_Burnham
Super User

Re: Removing the end of a string of variable length

Something like Left(:Protocol, Length(:Protocol) - 4)
-Dave

View solution in original post

Highlighted
DanAlexander
Level II

Re: Removing the end of a string of variable length

Thank you, I tried your solution and it worked perfectly.
Highlighted
JLX
JLX
Level II

Re: Removing the end of a string of variable length

I agree with @David_Burnham , you can get results by directly using left, if you really wanna use word, maybe you can refer to this one below (I also attached data table in attachment):

Names Default To Here( 1 );
Clear Log();
dt = Current Data Table();
new_col = dt << New Column( "Trimmed Protocol",
	character,
	formula( Word( [1 -2], :Protocol, "-" ) )
);
Highlighted

Re: Removing the end of a string of variable length

I thought of using the Left() function, too. Here is my take:

 

Names Default to Here( 1 );

string = "AAA-BB-1234-19-001";

Left( string, Contains( string, "00") - 2 );

I recommend that you select Help > Scripting Index and select Functions > Character. There is a lot of built-in support for manipulating character strings.

 

Learn it once, use it forever!
Article Labels