Count characters in a string variable without blank

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Count characters in a string variable without blank

huang jialin
Hi Everyone,

I was trying to count the number of characters in a string variable, which includes blanks. I tried to use CHAR.LENGTH( Response ), it yields 38 for the example. Then, I tried LENGTH(RTRIM( Response , " ")),which gave the same results.

However, what I am looking for is how to only count characters without strings. The expected result should be 26 (including # sign).

Your advice will be very appreciated. Thank you very much.

Example:
Name of the variable : Response

#BADA CDACDA   D BDAD    BBACBCA   ABB

Sincerely,
Jialin Huang
Reply | Threaded
Open this post in threaded view
|

Re: Count characters in a string variable without blank

Maguin, Eugene

I assume all character strings contain US keyboard printing characters.

Let x by the variable with format A50

Compute nchars=0.

Loop #i=1 to 50.

+  if (substr(x,i,1) ne ‘ ‘) nchar=nchar+1.

End loop.

 

Can it be done in fewer commands???

 

Gene Maguin

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of huang jialin
Sent: Thursday, September 27, 2012 5:11 PM
To: [hidden email]
Subject: Count characters in a string variable without blank

 

Hi Everyone,

I was trying to count the number of characters in a string variable, which includes blanks. I tried to use CHAR.LENGTH( Response ), it yields 38 for the example. Then, I tried LENGTH(RTRIM( Response , " ")),which gave the same results.

However, what I am looking for is how to only count characters without strings. The expected result should be 26 (including # sign).

Your advice will be very appreciated. Thank you very much.

Example:
Name of the variable : Response

#BADA CDACDA   D BDAD    BBACBCA   ABB

Sincerely,
Jialin Huang

Reply | Threaded
Open this post in threaded view
|

Re: Count characters in a string variable without blank

Jon K Peck
compute nchars = char.length(replace(x, " ", ""))

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        "Maguin, Eugene" <[hidden email]>
To:        [hidden email]
Date:        09/27/2012 03:24 PM
Subject:        Re: [SPSSX-L] Count characters in a string variable without blank
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I assume all character strings contain US keyboard printing characters.
Let x by the variable with format A50
Compute nchars=0.
Loop #i=1 to 50.
+  if (substr(x,i,1) ne ‘ ‘) nchar=nchar+1.
End loop.
 
Can it be done in fewer commands???
 
Gene Maguin
 
From: SPSSX(r) Discussion [[hidden email]] On Behalf Of huang jialin
Sent:
Thursday, September 27, 2012 5:11 PM
To:
[hidden email]
Subject:
Count characters in a string variable without blank

 
Hi Everyone,

I was trying to count the number of characters in a string variable, which includes blanks. I tried to use CHAR.LENGTH( Response ), it yields 38 for the example. Then, I tried LENGTH(RTRIM( Response , " ")),which gave the same results.

However, what I am looking for is how to only count characters without strings. The expected result should be 26 (including # sign).

Your advice will be very appreciated. Thank you very much.

Example:
Name of the variable : Response

#BADA CDACDA   D BDAD    BBACBCA   ABB

Sincerely,
Jialin Huang

Reply | Threaded
Open this post in threaded view
|

Re: Count characters in a string variable without blank

David Marso
Administrator
In reply to this post by Maguin, Eugene

"Can it be done in fewer commands???"
YEP!!!!! ;-))))))

COMPUTE L=LENGTH(RTRIM(REPLACE(X," ",""))).
===========

DATA LIST /X (A100).
BEGIN DATA
#BADA CDACDA   D BDAD    BBACBCA   ABB
END DATA.

COMPUTE L=LENGTH(RTRIM(REPLACE(X," ",""))).

LIST.


Maguin, Eugene wrote
I assume all character strings contain US keyboard printing characters.
Let x by the variable with format A50
Compute nchars=0.
Loop #i=1 to 50.
+  if (substr(x,i,1) ne ‘ ‘) nchar=nchar+1.
End loop.

Can it be done in fewer commands???

Gene Maguin

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of huang jialin
Sent: Thursday, September 27, 2012 5:11 PM
To: [hidden email]
Subject: Count characters in a string variable without blank

Hi Everyone,

I was trying to count the number of characters in a string variable, which includes blanks. I tried to use CHAR.LENGTH( Response ), it yields 38 for the example. Then, I tried LENGTH(RTRIM( Response , " ")),which gave the same results.

However, what I am looking for is how to only count characters without strings. The expected result should be 26 (including # sign).

Your advice will be very appreciated. Thank you very much.

Example:
Name of the variable : Response

#BADA CDACDA   D BDAD    BBACBCA   ABB

Sincerely,
Jialin Huang
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Count characters in a string variable without blank

David Marso
Administrator
In reply to this post by huang jialin
RTRIM simply strips trailing blanks but does NOT do anything with embedded spaces.
See solution below.
COMPUTE L=LENGTH(RTRIM(REPLACE(X," ",""))).

huang jialin wrote
Hi Everyone,

I was trying to count the number of characters in a string variable, which
includes blanks. I tried to use CHAR.LENGTH( Response ), it yields 38 for
the example. Then, I tried LENGTH(RTRIM( Response , " ")),which gave the
same results.

However, what I am looking for is how to only count characters without
strings. The expected result should be 26 (including # sign).

Your advice will be very appreciated. Thank you very much.

Example:
Name of the variable : Response

#BADA CDACDA   D BDAD    BBACBCA   ABB

Sincerely,
Jialin Huang
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Count characters in a string variable without blank

huang jialin
Hi Everyone,

Your solutions worked. Thank you very much for your helps.

But I have a question. When I used REPLACE(X," ", "") to remove the blanks. SPSS showed an error " Character strings may only be assigned to string variables." But the variable is actually string. However, David and Jon solutions involved REPLACE function. There is no error. What is going on there?

Thank you again.

Sincerely,
Jialin Huang


On Thu, Sep 27, 2012 at 4:55 PM, David Marso <[hidden email]> wrote:
RTRIM simply strips trailing blanks but does NOT do anything with embedded
spaces.
See solution below.
COMPUTE L=LENGTH(RTRIM(REPLACE(X," ",""))).


huang jialin wrote
> Hi Everyone,
>
> I was trying to count the number of characters in a string variable, which
> includes blanks. I tried to use CHAR.LENGTH( Response ), it yields 38 for
> the example. Then, I tried LENGTH(RTRIM( Response , " ")),which gave the
> same results.
>
> However, what I am looking for is how to only count characters without
> strings. The expected result should be 26 (including # sign).
>
> Your advice will be very appreciated. Thank you very much.
>
> Example:
> Name of the variable : Response
>
> #BADA CDACDA   D BDAD    BBACBCA   ABB
>
> Sincerely,
> Jialin Huang





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Count-characters-in-a-string-variable-without-blank-tp5715332p5715337.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

Reply | Threaded
Open this post in threaded view
|

Re: Count characters in a string variable without blank

Jon K Peck
The error does not have to do with replace.  It is telling you that you are trying to assign a string value to a variable that has not been declared as string type.  New variables are by default numeric, so you need to declare a string variable and a length code for it.
E.g.,
string huang(a50).
compute huang = <some string expression>.



Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        huang jialin <[hidden email]>
To:        [hidden email]
Date:        09/28/2012 07:33 AM
Subject:        Re: [SPSSX-L] Count characters in a string variable without blank
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi Everyone,

Your solutions worked. Thank you very much for your helps.

But I have a question. When I used REPLACE(X," ", "") to remove the blanks. SPSS showed an error " Character strings may only be assigned to string variables." But the variable is actually string. However, David and Jon solutions involved REPLACE function. There is no error. What is going on there?

Thank you again.

Sincerely,
Jialin Huang


On Thu, Sep 27, 2012 at 4:55 PM, David Marso <david.marso@...> wrote:
RTRIM simply strips trailing blanks but does NOT do anything with embedded
spaces.
See solution below.
COMPUTE L=LENGTH(RTRIM(REPLACE(X," ",""))).


huang jialin wrote

> Hi Everyone,
>
> I was trying to count the number of characters in a string variable, which
> includes blanks. I tried to use CHAR.LENGTH( Response ), it yields 38 for
> the example. Then, I tried LENGTH(RTRIM( Response , " ")),which gave the
> same results.
>
> However, what I am looking for is how to only count characters without
> strings. The expected result should be 26 (including # sign).
>
> Your advice will be very appreciated. Thank you very much.
>
> Example:
> Name of the variable : Response
>
> #BADA CDACDA   D BDAD    BBACBCA   ABB
>
> Sincerely,
> Jialin Huang





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
--

View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Count-characters-in-a-string-variable-without-blank-tp5715332p5715337.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to

LISTSERV@... (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD