BookmarkSubscribeRSS Feed
shahram

Community Trekker

Joined:

Nov 29, 2014

Count the number of words in a string?

Can someone tell me the best function to count the number of a words in a particular string?  For example, if I have a column of character data delimited by ";" or "," how do I count the number of "words" in that column, as defined by the particular delimiter?  I know SAS has a function COUNTW that does this, but I can't find anything in JMP that will do it.  Thanks in advance...

1 ACCEPTED SOLUTION

Accepted Solutions
ms

Super User

Joined:

Jun 23, 2011

Solution

Re: Count the number of words in a string?

It takes two functions in JMP. The Words() function returns a list of the words in a string and N Items() gives the number of  items in a list.

N Items( Words( s, ";,"))

9 REPLIES
ms

Super User

Joined:

Jun 23, 2011

Solution

Re: Count the number of words in a string?

It takes two functions in JMP. The Words() function returns a list of the words in a string and N Items() gives the number of  items in a list.

N Items( Words( s, ";,"))

shahram

Community Trekker

Joined:

Nov 29, 2014

Re: Count the number of words in a string?

Great, thanks a lot.

l_yampolsky

Occasional Contributor

Joined:

Jan 3, 2018

Re: Count the number of words in a string?

Does the function N Items exist only in JMP Pro?

Is there a way to count words or specific symbols in a string in regular JMP?

txnelson

Super User

Joined:

Jun 22, 2012

Re: Count the number of words in a string?

N Items() is a function that has been a part of JMP for a long time.  Check in the Scripting Index of your version of JMP to make sure it is there.  I assume it will be.  It is an essential component of being able to deal with JMP Lists.

Here is one way to count words and to count symbols

Names Default To Here( 1 );
str1 = "This is a test of the counting of words";
str2 = "Here is* a way to* count * symbols";
Show( "Counting Words", N Items( Words( str1, " " ) ) );
Show( "Counting Symbols", N Items( Words( str2, "*" ) ) );

 

 

Jim
l_yampolsky

Occasional Contributor

Joined:

Jan 3, 2018

Re: Count the number of words in a string?

Thanks! It does not appear in the pull-down function lists. But of course when I just type "N Items(.." etc. it works.

txnelson

Super User

Joined:

Jun 22, 2012

Re: Count the number of words in a string?

I assume the pulldown menu you are referring to is in the Formula Editor.  That list is not the definative list of functions.  It contains those functions that "Typically" are used in a formula calculation.  The definative documentation is in the Scripting Index

     Help==>Scripting Index

nitems.PNG

Jim
l_yampolsky

Occasional Contributor

Joined:

Jan 3, 2018

Re: Count the number of words in a string?

Yep, got it, thanks. Yeah, I had no idea that there are functions that are not in the function editor. Using JMP since ver 1, 1990-ies. Go figure.

pmroz

Super User

Joined:

Jun 23, 2011

Re: Count the number of words in a string?

Minor point - your symbol count could be off by 1 if the last character is not the symbol.  In Jim's example the result is 4 when there are actually only 3 symbols.  Here's one way to get the correct number of symbols:

str2 = "Here is* a way to* count * symbols";
str3 = substitute(str2, "*", "");
nsymbols = length(str2) - length(str3);

I'm curious if there's an easier way to do this?

l_yampolsky

Occasional Contributor

Joined:

Jan 3, 2018

Re: Count the number of words in a string?

this method did occur to me, but it was not erfect for my goal of counting words in a string with words separated by a variety of delimiters, would have had to do this several time for each delimiter.

N Items(Words(string, " ,;|:_")) works great.