deleting blanks in string variables

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

deleting blanks in string variables

drfg2008
I would like to delete blanks in a string variable. The data comes from a DB that put blanks before, in between and at the end of the actual string like:

"   one"
"   two"
"   three"
"four   "
"five   "
"   six   "
"   seven  "

so I tried:

COMPUTE new_var=REPLACE(old_var," ").
EXECUTE.

this seems to be a bit naive.

(I'm not even quite sure if there are always 3 blanks in the beginning, or in the end, or in between)

- probably  a solution with a loop?  -  Can't find a solution.

Thanks.
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

AW: deleting blanks in string variables

Bailer Harald-2
Frank, what about something like:

COMPUTE new = RTRIM((LTRIM(old))) .

Cheers
Harald

-----Ursprüngliche Nachricht-----
Von: SPSSX(r) Discussion [mailto:[hidden email]] Im Auftrag von drfg2008
Gesendet: Freitag, 8. Juli 2011 10:55
An: [hidden email]
Betreff: deleting blanks in string variables

I would like to delete blanks in a string variable. The data comes from a DB
that put blanks before, in between and at the end of the actual string like:

"   one"
"   two"
"   three"
"four   "
"five   "
"   six   "
"   seven  "

so I tried:

COMPUTE new_var=REPLACE(old_var," ").
EXECUTE.

this seems to be a bit naive.

(I'm not even quite sure if there are always 3 blanks in the beginning, or
in the end, or in between)

- probably  a solution with a loop?  -  Can't find a solution.

Thanks.

-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/deleting-blanks-in-string-variables-tp4563967p4563967.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

=====================
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: deleting blanks in string variables

Bruce Weaver
Administrator
In reply to this post by drfg2008
You didn't specify what to replace " " with.  Try this:

COMPUTE new_var=REPLACE(old_var," ","").


drfg2008 wrote
I would like to delete blanks in a string variable. The data comes from a DB that put blanks before, in between and at the end of the actual string like:

"   one"
"   two"
"   three"
"four   "
"five   "
"   six   "
"   seven  "

so I tried:

COMPUTE new_var=REPLACE(old_var," ").
EXECUTE.

this seems to be a bit naive.

(I'm not even quite sure if there are always 3 blanks in the beginning, or in the end, or in between)

- probably  a solution with a loop?  -  Can't find a solution.

Thanks.
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: deleting blanks in string variables

David Marso
Administrator
In reply to this post by drfg2008

STRING new_var (A8).
COMPUTE new_var=LTRIM(old_var).
* Someone else posted RTRIM(LTRIM(...)) but RTRIM is really only relevant if you are attempting to concatenate 2 strings and don't desire extra blanks between the elements.
REPLACE is fine but really designed for replacing one thing with something else.

"- probably  a solution with a loop?  -  Can't find a solution."
Frank, KISS!  and RTFM!


drfg2008 wrote
I would like to delete blanks in a string variable. The data comes from a DB that put blanks before, in between and at the end of the actual string like:

"   one"
"   two"
"   three"
"four   "
"five   "
"   six   "
"   seven  "

so I tried:

COMPUTE new_var=REPLACE(old_var," ").
EXECUTE.

this seems to be a bit naive.

(I'm not even quite sure if there are always 3 blanks in the beginning, or in the end, or in between)

- probably  a solution with a loop?  -  Can't find a solution.

Thanks.
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: deleting blanks in string variables

Maguin, Eugene
In reply to this post by Bruce Weaver
I'm curious about this. When you do what Bruce suggested,

COMPUTE new_var=REPLACE(old_var," ","").

what is the character that replaces the " "? Is it another space (decimal
32/hex 20)? Something else?

Gene Maguin


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Bruce Weaver
Sent: Friday, July 08, 2011 7:14 AM
To: [hidden email]
Subject: Re: deleting blanks in string variables

You didn't specify what to replace " " with.  Try this:

COMPUTE new_var=REPLACE(old_var," ","").



drfg2008 wrote:

>
> I would like to delete blanks in a string variable. The data comes from a
> DB that put blanks before, in between and at the end of the actual string
> like:
>
> "   one"
> "   two"
> "   three"
> "four   "
> "five   "
> "   six   "
> "   seven  "
>
> so I tried:
>
> COMPUTE new_var=REPLACE(old_var," ").
> EXECUTE.
>
> this seems to be a bit naive.
>
> (I'm not even quite sure if there are always 3 blanks in the beginning, or
> in the end, or in between)
>
> - probably  a solution with a loop?  -  Can't find a solution.
>
> Thanks.
>


-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/deleting-blanks-in-string-vari
ables-tp4563967p4564392.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

=====================
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: deleting blanks in string variables

drfg2008
thanks everyone, however I get the following error message:

>Fehler Nr.  4381 in Spalte 44.  Text: .
>Unerwartetes Ende des Ausdrucks.
>Dieser Befehl wird nicht ausgeführt.

FirstMeet_t4 is a string variable
this is the code:

STRING FirstMeet_t4new (A8).

COMPUTE FirstMeet_t4new=LTRIM(FirstMeet_t4).

or ...
COMPUTE FirstMeet_t4new = RTRIM((LTRIM(FirstMeet_t4))) .

or ...
COMPUTE FirstMeet_t4new=REPLACE(FirstMeet_t4," ","").
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Automatic reply: deleting blanks in string variables

Buhi, Eric
Banned User
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: deleting blanks in string variables

John F Hall
In reply to this post by Maguin, Eugene
I suspect it's the same as find/replace, so it just takes the blanks out.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Gene Maguin
Sent: 08 July 2011 16:18
To: [hidden email]
Subject: Re: deleting blanks in string variables

I'm curious about this. When you do what Bruce suggested,

COMPUTE new_var=REPLACE(old_var," ","").

what is the character that replaces the " "? Is it another space (decimal
32/hex 20)? Something else?

Gene Maguin


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Bruce Weaver
Sent: Friday, July 08, 2011 7:14 AM
To: [hidden email]
Subject: Re: deleting blanks in string variables

You didn't specify what to replace " " with.  Try this:

COMPUTE new_var=REPLACE(old_var," ","").



drfg2008 wrote:

>
> I would like to delete blanks in a string variable. The data comes from a
> DB that put blanks before, in between and at the end of the actual string
> like:
>
> "   one"
> "   two"
> "   three"
> "four   "
> "five   "
> "   six   "
> "   seven  "
>
> so I tried:
>
> COMPUTE new_var=REPLACE(old_var," ").
> EXECUTE.
>
> this seems to be a bit naive.
>
> (I'm not even quite sure if there are always 3 blanks in the beginning, or
> in the end, or in between)
>
> - probably  a solution with a loop?  -  Can't find a solution.
>
> Thanks.
>


-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/deleting-blanks-in-string-vari
ables-tp4563967p4564392.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

=====================
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

=====================
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: deleting blanks in string variables

drfg2008
In reply to this post by David Marso
I get this error message:

>Fehler Nr.  4381 in Spalte 44.  Text: .
>Unerwartetes Ende des Ausdrucks.
>Dieser Befehl wird nicht ausgeführt.


Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: deleting blanks in string variables

Maguin, Eugene
In reply to this post by drfg2008
Hi Frank,

The problem text seems to be a period. But, what is the english text of the
German error text?

You have three different computations there. Which one gave the error?

Gene Maguin

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
drfg2008
Sent: Friday, July 08, 2011 12:21 PM
To: [hidden email]
Subject: Re: deleting blanks in string variables

thanks everyone, however I get the following error message:

>Fehler Nr.  4381 in Spalte 44.  Text: .
>Unerwartetes Ende des Ausdrucks.
>Dieser Befehl wird nicht ausgeführt.

FirstMeet_t4 is a string variable
this is the code:

STRING FirstMeet_t4new (A8).

COMPUTE FirstMeet_t4new=LTRIM(FirstMeet_t4).

or ...
COMPUTE FirstMeet_t4new = RTRIM((LTRIM(FirstMeet_t4))) .

or ...
COMPUTE FirstMeet_t4new=REPLACE(FirstMeet_t4," ","").


-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/deleting-blanks-in-string-vari
ables-tp4563967p4565390.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

=====================
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: deleting blanks in string variables

Bruce Weaver
Administrator
In reply to this post by drfg2008
The following runs without errors for me (v18 under Windoze XP).

new file.
dataset close all.

data list list / FirstMeet_t4 (a12).
begin data
"   one"
"   two"
"   three"
"four   "
"five   "
"   six   "
"   seven  "
end data.

STRING FirstMeet_t4new (A8).
COMPUTE FirstMeet_t4new=REPLACE(FirstMeet_t4," ","").
list.

drfg2008 wrote
thanks everyone, however I get the following error message:

>Fehler Nr.  4381 in Spalte 44.  Text: .
>Unerwartetes Ende des Ausdrucks.
>Dieser Befehl wird nicht ausgeführt.

FirstMeet_t4 is a string variable
this is the code:

STRING FirstMeet_t4new (A8).

COMPUTE FirstMeet_t4new=LTRIM(FirstMeet_t4).

or ...
COMPUTE FirstMeet_t4new = RTRIM((LTRIM(FirstMeet_t4))) .

or ...
COMPUTE FirstMeet_t4new=REPLACE(FirstMeet_t4," ","").
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: deleting blanks in string variables

drfg2008
thanks Bruce,


now I found the reason why your script -which is exactly same as my script- works, but my sript does not (on SPSS 17).

The reason is a blank behind the end of a compute command. Behind the dot (!). You can't see a blank behind a command and get mad.

It already happened to me a few times (only with the compute command), and I never figured out the reason. Now I know. How simple.


Frank
Dr. Frank Gaeth