Search and replace

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

Search and replace

emma78
Hi, I have one short question:
I  have two variables:


DEST_STREET               housenumber
teststr.40                               40                  
40 teststr                               40                  
rue de la fdsffs 32                     32                  
46 rue de la fdsffs                     46                  
rue de 47 la fdsffs                     47                  


I want to create a new variable STREET and search for the value of housenumber and replace it with nothing

COMPUTE STREET = REPLACE(DEST_STREET,housenumber,"").

STREET (should look like this)
teststr.                                
teststr                              
rue de la fdsffs                        
rue de la fdsffs                    
rue de la fdsffs                    

Unfortunately my result with this syntax is
teststr.                                
40 teststr                              
rue de la fdsffs                        
46 rue de la fdsffs                    
rue de 47 la fdsffs      

Only in the rows where the number is at the end, it is working. Do you have any ideas? :-)
             
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

Andy W
My guess in housenumber has leading and/or trailing blanks. Try

COMPUTE STREET = REPLACE(DEST_STREET,LTRIM(RTRIM(housenumber)),"").
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

emma78
Thank you,
now it looks like this:

teststr.                                
 teststr                                
rue de la fdsffs                        
 rue de la fdsffs                      
rue de 45 la fdsffs    


The number in the middle is still there:-)                  
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

Jon Peck
If you just want to get rid of all the digits in the street variable, this will do it.
spssinc trans result =street type=30
/formula "re.sub('\d+', '', DEST_STREET)".

It searches for one or more digits and replaces them with nothing.  type=30 says the result variable is A30.  Change as needed.

On Tue, Jul 5, 2016 at 9:50 AM, emma78 <[hidden email]> wrote:
Thank you,
now it looks like this:

teststr.
 teststr
rue de la fdsffs
 rue de la fdsffs
rue de 45 la fdsffs


The number in the middle is still there:-)



--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Search-and-replace-tp5732598p5732600.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



--
Jon K Peck
[hidden email]

===================== 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: Search and replace

PRogman
In reply to this post by Andy W
You could further improve the output by removing leading/trailing blanks or double spaces left when replacing the housenumber (which I assume is type STRING).

COMPUTE STREET = LTRIM(RTRIM(REPLACE(REPLACE(DEST_STREET,LTRIM(RTRIM(housenumber)),""),"  "," "))).


Andy W wrote
My guess in housenumber has leading and/or trailing blanks. Try

COMPUTE STREET = REPLACE(DEST_STREET,LTRIM(RTRIM(housenumber)),"").
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

emma78
In reply to this post by Jon Peck
Cool, thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

David Marso
Administrator
In reply to this post by emma78
Presumably housenumber is a string variable?
Maybe  RTRIM and LTRIM in the mix would suffice?
STRING Street (A30).
COMPUTE Street=REPLACE(DEST_STREET,LTRIM(RTRIM(housenumber)),"").

emma78 wrote
Hi, I have one short question:
I  have two variables:


DEST_STREET               housenumber
teststr.40                               40                  
40 teststr                               40                  
rue de la fdsffs 32                     32                  
46 rue de la fdsffs                     46                  
rue de 47 la fdsffs                     47                  


I want to create a new variable STREET and search for the value of housenumber and replace it with nothing

COMPUTE STREET = REPLACE(DEST_STREET,housenumber,"").

STREET (should look like this)
teststr.                                
teststr                              
rue de la fdsffs                        
rue de la fdsffs                    
rue de la fdsffs                    

Unfortunately my result with this syntax is
teststr.                                
40 teststr                              
rue de la fdsffs                        
46 rue de la fdsffs                    
rue de 47 la fdsffs      

Only in the rows where the number is at the end, it is working. Do you have any ideas? :-)
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: Search and replace

Bruce Weaver
Administrator
In reply to this post by emma78
Jon's SPSSINC TRANS code no doubt works very well.  And I've discovered that one can bring up a dialog to generate that command by clicking on Transform > Programmability Transformation.  But it seems to me that users who are not quite familiar with Python will not have any idea what to plug in to the Transformation Formula box.  

Meanwhile, here is another solution that uses native SPSS syntax only.

* Read in the sample data.
DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
BEGIN DATA
teststr.40              40                  
40 teststr              40                  
rue de la fdsffs 32     32                  
46 rue de la fdsffs     46                  
rue de 47 la fdsffs     47                  
END DATA.

STRING Street (A20) #digits(A10) #d(A1).
COMPUTE #digits = '0123456789'.
COMPUTE Street = DEST_STREET.
DO REPEAT i = 1 to 10.
- COMPUTE #d = CHAR.SUBSTRING(#digits,i,1).
- IF CHAR.INDEX(DEST_STREET,#d) GT 0 Street = REPLACE(Street,#d,'').
END REPEAT.
COMPUTE Street = LTRIM(REPLACE(Street,"  "," ")).
LIST.

Output from LIST:

DEST_STREET          housenumber Street
 
teststr.40                40     teststr.
40 teststr                40     teststr
rue de la fdsffs 32       32     rue de la fdsffs
46 rue de la fdsffs       46     rue de la fdsffs
rue de 47 la fdsffs       47     rue de la fdsffs
 
Number of cases read:  5    Number of cases listed:  5

HTH.

emma78 wrote
Thank you,
now it looks like this:

teststr.                                
 teststr                                
rue de la fdsffs                        
 rue de la fdsffs                      
rue de 45 la fdsffs    


The number in the middle is still there:-)
--
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: Search and replace

David Marso
Administrator
This also works ;-)
Second REPLACE to remove extra space.
--
DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
BEGIN DATA
teststr.40              40                  
40 teststr              40                  
rue de la fdsffs 32     32                  
46 rue de la fdsffs     46                  
rue de 47 la fdsffs     47                  
END DATA.

STRING Street (A20).
COMPUTE Street=LTRIM(REPLACE(REPLACE(DEST_STREET ,STRING(housenumber,F2),""),"  "," ")).
LIST.

Bruce Weaver wrote
Jon's SPSSINC TRANS code no doubt works very well.  And I've discovered that one can bring up a dialog to generate that command by clicking on Transform > Programmability Transformation.  But it seems to me that users who are not quite familiar with Python will not have any idea what to plug in to the Transformation Formula box.  

Meanwhile, here is another solution that uses native SPSS syntax only.

* Read in the sample data.
DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
BEGIN DATA
teststr.40              40                  
40 teststr              40                  
rue de la fdsffs 32     32                  
46 rue de la fdsffs     46                  
rue de 47 la fdsffs     47                  
END DATA.

STRING Street (A20) #digits(A10) #d(A1).
COMPUTE #digits = '0123456789'.
COMPUTE Street = DEST_STREET.
DO REPEAT i = 1 to 10.
- COMPUTE #d = CHAR.SUBSTRING(#digits,i,1).
- IF CHAR.INDEX(DEST_STREET,#d) GT 0 Street = REPLACE(Street,#d,'').
END REPEAT.
COMPUTE Street = LTRIM(REPLACE(Street,"  "," ")).
LIST.

Output from LIST:

DEST_STREET          housenumber Street
 
teststr.40                40     teststr.
40 teststr                40     teststr
rue de la fdsffs 32       32     rue de la fdsffs
46 rue de la fdsffs       46     rue de la fdsffs
rue de 47 la fdsffs       47     rue de la fdsffs
 
Number of cases read:  5    Number of cases listed:  5

HTH.

emma78 wrote
Thank you,
now it looks like this:

teststr.                                
 teststr                                
rue de la fdsffs                        
 rue de la fdsffs                      
rue de 45 la fdsffs    


The number in the middle is still there:-)
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: Search and replace

Jon Peck
In reply to this post by Bruce Weaver
No doubt, using the real power of Python here requires writing a smidgen of Python code.  The dialog really just advertises the command.  But in many cases just a tiny bit of code will do.  It helps to have a Python-savvy friend (who doesn't need to know anything about SPSS).

On Tuesday, July 5, 2016, Bruce Weaver <[hidden email]> wrote:
Jon's SPSSINC TRANS code no doubt works very well.  And I've discovered that
one can bring up a dialog to generate that command by clicking on *Transform
> Programmability Transformation*.  But it seems to me that users who are
not quite familiar with Python will not have any idea what to plug in to the
Transformation Formula box.

Meanwhile, here is another solution that uses native SPSS syntax only.

* Read in the sample data.
DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
BEGIN DATA
teststr.40              40
40 teststr              40
rue de la fdsffs 32     32
46 rue de la fdsffs     46
rue de 47 la fdsffs     47
END DATA.

STRING Street (A20) #digits(A10) #d(A1).
COMPUTE #digits = '0123456789'.
COMPUTE Street = DEST_STREET.
DO REPEAT i = 1 to 10.
- COMPUTE #d = CHAR.SUBSTRING(#digits,i,1).
- IF CHAR.INDEX(DEST_STREET,#d) GT 0 Street = REPLACE(Street,#d,'').
END REPEAT.
COMPUTE Street = LTRIM(REPLACE(Street,"  "," ")).
LIST.

Output from LIST:

DEST_STREET          housenumber Street

teststr.40                40     teststr.
40 teststr                40     teststr
rue de la fdsffs 32       32     rue de la fdsffs
46 rue de la fdsffs       46     rue de la fdsffs
rue de 47 la fdsffs       47     rue de la fdsffs

Number of cases read:  5    Number of cases listed:  5

HTH.


emma78 wrote
> Thank you,
> now it looks like this:
>
> teststr.
>  teststr
> rue de la fdsffs
>  rue de la fdsffs
> rue de 45 la fdsffs
>
>
> The number in the middle is still there:-)





-----
--
Bruce Weaver
<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;bweaver@lakeheadu.ca&#39;)">bweaver@...
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/Search-and-replace-tp5732598p5732605.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;LISTSERV@LISTSERV.UGA.EDU&#39;)">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


--
Jon K Peck
[hidden email]


===================== 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: Search and replace

Bruce Weaver
Administrator
In reply to this post by David Marso
Very nice, David!  But I think it breaks down if the house numbers are not all 2 digits.  

In the following example with house numbers from 1 to 5 digits, changing STRING(housenumber,F5) to LTRIM(RTRIM(STRING(housenumber,F5))) seems to fix it.  


DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-29.
BEGIN DATA
rue de la fdsffs 1          1                  
rue de la fdsffs 21        21                  
321 rue de la fdsffs      321                  
rue de 4321 la fdsffs    4321                  
rue de 54321 la fdsffs  54321
END DATA.

* BW's clunky method.
STRING Street (A20) #digits(A10) #d(A1).
COMPUTE #digits = '0123456789'.
COMPUTE Street = DEST_STREET.
DO REPEAT i = 1 to 10.
- COMPUTE #d = CHAR.SUBSTRING(#digits,i,1).
- IF CHAR.INDEX(DEST_STREET,#d) GT 0 Street = REPLACE(Street,#d,'').
END REPEAT.
COMPUTE Street = LTRIM(REPLACE(Street,"  "," ")).

* DM's syntax, with F2 changed to F5.
STRING Street2 (A20).
COMPUTE Street2=LTRIM(REPLACE(REPLACE(DEST_STREET ,STRING(housenumber,F5),""),"  "," ")).
* Modification of DM's method with LTRIM(RTRIM()) added.
STRING Street3 (A20).
COMPUTE Street3=LTRIM(REPLACE(REPLACE(DEST_STREET ,LTRIM(RTRIM(STRING(housenumber,F5))),""),"  "," ")).
LIST.

Output from LIST:

Street               Street2              Street3
 
rue de la fdsffs     rue de la fdsffs 1   rue de la fdsffs
rue de la fdsffs     rue de la fdsffs 21  rue de la fdsffs
rue de la fdsffs     321 rue de la fdsffs rue de la fdsffs
rue de la fdsff      rue de la fdsff      rue de la fdsff
rue de la fdsf       rue de la fdsf       rue de la fdsf
 
Number of cases read:  5    Number of cases listed:  5


David Marso wrote
This also works ;-)
Second REPLACE to remove extra space.
--
DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
BEGIN DATA
teststr.40              40                  
40 teststr              40                  
rue de la fdsffs 32     32                  
46 rue de la fdsffs     46                  
rue de 47 la fdsffs     47                  
END DATA.

STRING Street (A20).
COMPUTE Street=LTRIM(REPLACE(REPLACE(DEST_STREET ,STRING(housenumber,F2),""),"  "," ")).
LIST.

Bruce Weaver wrote
Jon's SPSSINC TRANS code no doubt works very well.  And I've discovered that one can bring up a dialog to generate that command by clicking on Transform > Programmability Transformation.  But it seems to me that users who are not quite familiar with Python will not have any idea what to plug in to the Transformation Formula box.  

Meanwhile, here is another solution that uses native SPSS syntax only.

* Read in the sample data.
DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
BEGIN DATA
teststr.40              40                  
40 teststr              40                  
rue de la fdsffs 32     32                  
46 rue de la fdsffs     46                  
rue de 47 la fdsffs     47                  
END DATA.

STRING Street (A20) #digits(A10) #d(A1).
COMPUTE #digits = '0123456789'.
COMPUTE Street = DEST_STREET.
DO REPEAT i = 1 to 10.
- COMPUTE #d = CHAR.SUBSTRING(#digits,i,1).
- IF CHAR.INDEX(DEST_STREET,#d) GT 0 Street = REPLACE(Street,#d,'').
END REPEAT.
COMPUTE Street = LTRIM(REPLACE(Street,"  "," ")).
LIST.

Output from LIST:

DEST_STREET          housenumber Street
 
teststr.40                40     teststr.
40 teststr                40     teststr
rue de la fdsffs 32       32     rue de la fdsffs
46 rue de la fdsffs       46     rue de la fdsffs
rue de 47 la fdsffs       47     rue de la fdsffs
 
Number of cases read:  5    Number of cases listed:  5

HTH.

emma78 wrote
Thank you,
now it looks like this:

teststr.                                
 teststr                                
rue de la fdsffs                        
 rue de la fdsffs                      
rue de 45 la fdsffs    


The number in the middle is still there:-)
--
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: Search and replace

John F Hall
. . but what if the address happens to be 80bis rue du 6 Juin 1944?
There are many such in Normandy to commemorate D-Day.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf
Of Bruce Weaver
Sent: 05 July 2016 21:54
To: [hidden email]
Subject: Re: Search and replace

Very nice, David!  But I think it breaks down if the house numbers are
not all 2 digits.  

In the following example with house numbers from 1 to 5 digits,
changing
STRING(housenumber,F5) to LTRIM(RTRIM(STRING(housenumber,F5))) seems
to fix it.  


DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-29.
BEGIN DATA
rue de la fdsffs 1          1                  
rue de la fdsffs 21        21                  
321 rue de la fdsffs      321                  
rue de 4321 la fdsffs    4321                  
rue de 54321 la fdsffs  54321
END DATA.

* BW's clunky method.
STRING Street (A20) #digits(A10) #d(A1).
COMPUTE #digits = '0123456789'.
COMPUTE Street = DEST_STREET.
DO REPEAT i = 1 to 10.
- COMPUTE #d = CHAR.SUBSTRING(#digits,i,1).
- IF CHAR.INDEX(DEST_STREET,#d) GT 0 Street = REPLACE(Street,#d,'').
END REPEAT.
COMPUTE Street = LTRIM(REPLACE(Street,"  "," ")).

* DM's syntax, with F2 changed to F5.
STRING Street2 (A20).
COMPUTE Street2=LTRIM(REPLACE(REPLACE(DEST_STREET
,STRING(housenumber,F5),""),"  "," ")).
* Modification of DM's method with LTRIM(RTRIM()) added.
STRING Street3 (A20).
COMPUTE Street3=LTRIM(REPLACE(REPLACE(DEST_STREET
,LTRIM(RTRIM(STRING(housenumber,F5))),""),"  "," ")).
LIST.

Output from LIST:

Street               Street2              Street3
 
rue de la fdsffs     rue de la fdsffs 1   rue de la fdsffs
rue de la fdsffs     rue de la fdsffs 21  rue de la fdsffs
rue de la fdsffs     321 rue de la fdsffs rue de la fdsffs
rue de la fdsff      rue de la fdsff      rue de la fdsff
rue de la fdsf       rue de la fdsf       rue de la fdsf
 
Number of cases read:  5    Number of cases listed:  5



David Marso wrote

> This also works ;-)
> Second REPLACE to remove extra space.
> --
> DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
> BEGIN DATA
> teststr.40              40                  
> 40 teststr              40                  
> rue de la fdsffs 32     32                  
> 46 rue de la fdsffs     46                  
> rue de 47 la fdsffs     47                  
> END DATA.
>
> STRING Street (A20).
> COMPUTE Street=LTRIM(REPLACE(REPLACE(DEST_STREET
> ,STRING(housenumber,F2),""),"  "," ")).
> LIST.
> Bruce Weaver wrote
>> Jon's SPSSINC TRANS code no doubt works very well.  And I've
>> discovered that one can bring up a dialog to generate that command
by
>> clicking on
*
>> Transform > Programmability Transformation
*
>> .  But it seems to me that users who are not quite familiar with
>> Python will not have any idea what to plug in to the Transformation
Formula box.
>>
>> Meanwhile, here is another solution that uses native SPSS syntax
only.

>>
>> * Read in the sample data.
>> DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
>> BEGIN DATA
>> teststr.40              40                  
>> 40 teststr              40                  
>> rue de la fdsffs 32     32                  
>> 46 rue de la fdsffs     46                  
>> rue de 47 la fdsffs     47                  
>> END DATA.
>>
>> STRING Street (A20) #digits(A10) #d(A1).
>> COMPUTE #digits = '0123456789'.
>> COMPUTE Street = DEST_STREET.
>> DO REPEAT i = 1 to 10.
>> - COMPUTE #d = CHAR.SUBSTRING(#digits,i,1).
>> - IF CHAR.INDEX(DEST_STREET,#d) GT 0 Street =
REPLACE(Street,#d,'').

>> END REPEAT.
>> COMPUTE Street = LTRIM(REPLACE(Street,"  "," ")).
>> LIST.
>>
>> Output from LIST:
>>
>> DEST_STREET          housenumber Street
>>  
>> teststr.40                40     teststr.
>> 40 teststr                40     teststr
>> rue de la fdsffs 32       32     rue de la fdsffs
>> 46 rue de la fdsffs       46     rue de la fdsffs
>> rue de 47 la fdsffs       47     rue de la fdsffs
>>  
>> Number of cases read:  5    Number of cases listed:  5
>>
>> HTH.
>> emma78 wrote
>>> Thank you,
>>> now it looks like this:
>>>
>>> teststr.                                
>>>  teststr                                
>>> rue de la fdsffs                        
>>>  rue de la fdsffs                      
>>> rue de 45 la fdsffs    
>>>
>>>
>>> The number in the middle is still there:-)





-----
--
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/Search-and-replace-tp573
2598p5732640.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: Search and replace

Bruce Weaver
Administrator
Try it, John.  The modified version of David's method still works, assuming 80 is the house number you want to remove.  (My method, on the other hand, removes all digits without reference to the housenumber variable, so will not give what you want here.)

DATA LIST FIXED / DEST_STREET 1-24 (A)   housenumber 26-30.
BEGIN DATA
rue de la fdsffs 1           1                  
rue de la fdsffs 21         21                  
321 rue de la fdsffs       321                  
rue de 4321 la fdsffs     4321                  
rue de 54321 la fdsffs   54321
80bis rue du 6 Juin 1944    80
END DATA.

* Modification of DM's method with LTRIM(RTRIM()) added.
STRING Street3 (A20).
COMPUTE Street3=LTRIM(REPLACE(REPLACE(DEST_STREET,LTRIM(RTRIM(STRING(housenumber,F5))),""),"  "," ")).
LIST.

OUTPUT:

DEST_STREET              housenumber Street3
 
rue de la fdsffs 1              1    rue de la fdsffs
rue de la fdsffs 21            21    rue de la fdsffs
321 rue de la fdsffs          321    rue de la fdsffs
rue de 4321 la fdsffs        4321    rue de la fdsffs
rue de 54321 la fdsffs      54321    rue de la fdsffs
80bis rue du 6 Juin 1944       80    bis rue du 6 Juin 19
 
Number of cases read:  6    Number of cases listed:  6

If you actually want to remove 80bis, then the housenumber variable in this example needs to be a string variable, and STRING(housenumber,F5) becomes just housenumber in the COMPUTE line.  


John F Hall wrote
. . but what if the address happens to be 80bis rue du 6 Juin 1944?
There are many such in Normandy to commemorate D-Day.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf
Of Bruce Weaver
Sent: 05 July 2016 21:54
To: [hidden email]
Subject: Re: Search and replace

Very nice, David!  But I think it breaks down if the house numbers are
not all 2 digits.  

In the following example with house numbers from 1 to 5 digits,
changing
STRING(housenumber,F5) to LTRIM(RTRIM(STRING(housenumber,F5))) seems
to fix it.  


DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-29.
BEGIN DATA
rue de la fdsffs 1          1                  
rue de la fdsffs 21        21                  
321 rue de la fdsffs      321                  
rue de 4321 la fdsffs    4321                  
rue de 54321 la fdsffs  54321
END DATA.

* BW's clunky method.
STRING Street (A20) #digits(A10) #d(A1).
COMPUTE #digits = '0123456789'.
COMPUTE Street = DEST_STREET.
DO REPEAT i = 1 to 10.
- COMPUTE #d = CHAR.SUBSTRING(#digits,i,1).
- IF CHAR.INDEX(DEST_STREET,#d) GT 0 Street = REPLACE(Street,#d,'').
END REPEAT.
COMPUTE Street = LTRIM(REPLACE(Street,"  "," ")).

* DM's syntax, with F2 changed to F5.
STRING Street2 (A20).
COMPUTE Street2=LTRIM(REPLACE(REPLACE(DEST_STREET
,STRING(housenumber,F5),""),"  "," ")).
* Modification of DM's method with LTRIM(RTRIM()) added.
STRING Street3 (A20).
COMPUTE Street3=LTRIM(REPLACE(REPLACE(DEST_STREET
,LTRIM(RTRIM(STRING(housenumber,F5))),""),"  "," ")).
LIST.

Output from LIST:

Street               Street2              Street3
 
rue de la fdsffs     rue de la fdsffs 1   rue de la fdsffs
rue de la fdsffs     rue de la fdsffs 21  rue de la fdsffs
rue de la fdsffs     321 rue de la fdsffs rue de la fdsffs
rue de la fdsff      rue de la fdsff      rue de la fdsff
rue de la fdsf       rue de la fdsf       rue de la fdsf
 
Number of cases read:  5    Number of cases listed:  5



David Marso wrote
> This also works ;-)
> Second REPLACE to remove extra space.
> --
> DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
> BEGIN DATA
> teststr.40              40                  
> 40 teststr              40                  
> rue de la fdsffs 32     32                  
> 46 rue de la fdsffs     46                  
> rue de 47 la fdsffs     47                  
> END DATA.
>
> STRING Street (A20).
> COMPUTE Street=LTRIM(REPLACE(REPLACE(DEST_STREET
> ,STRING(housenumber,F2),""),"  "," ")).
> LIST.
> Bruce Weaver wrote
>> Jon's SPSSINC TRANS code no doubt works very well.  And I've
>> discovered that one can bring up a dialog to generate that command
by
>> clicking on
*
>> Transform > Programmability Transformation
*
>> .  But it seems to me that users who are not quite familiar with
>> Python will not have any idea what to plug in to the Transformation
Formula box.
>>
>> Meanwhile, here is another solution that uses native SPSS syntax
only.
>>
>> * Read in the sample data.
>> DATA LIST FIXED / DEST_STREET 1-20 (A)   housenumber 25-26.
>> BEGIN DATA
>> teststr.40              40                  
>> 40 teststr              40                  
>> rue de la fdsffs 32     32                  
>> 46 rue de la fdsffs     46                  
>> rue de 47 la fdsffs     47                  
>> END DATA.
>>
>> STRING Street (A20) #digits(A10) #d(A1).
>> COMPUTE #digits = '0123456789'.
>> COMPUTE Street = DEST_STREET.
>> DO REPEAT i = 1 to 10.
>> - COMPUTE #d = CHAR.SUBSTRING(#digits,i,1).
>> - IF CHAR.INDEX(DEST_STREET,#d) GT 0 Street =
REPLACE(Street,#d,'').
>> END REPEAT.
>> COMPUTE Street = LTRIM(REPLACE(Street,"  "," ")).
>> LIST.
>>
>> Output from LIST:
>>
>> DEST_STREET          housenumber Street
>>  
>> teststr.40                40     teststr.
>> 40 teststr                40     teststr
>> rue de la fdsffs 32       32     rue de la fdsffs
>> 46 rue de la fdsffs       46     rue de la fdsffs
>> rue de 47 la fdsffs       47     rue de la fdsffs
>>  
>> Number of cases read:  5    Number of cases listed:  5
>>
>> HTH.
>> emma78 wrote
>>> Thank you,
>>> now it looks like this:
>>>
>>> teststr.                                
>>>  teststr                                
>>> rue de la fdsffs                        
>>>  rue de la fdsffs                      
>>> rue de 45 la fdsffs    
>>>
>>>
>>> The number in the middle is still there:-)





-----
--
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/Search-and-replace-tp573
2598p5732640.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
--
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/).