Search and replace

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

Search and replace

emma78
Hi,
sorry for the stupid question, but is it possible to do something like this:

This is my data:
oldstring     UniqueID
SW72DT                     2DT  
SW7 3DT                   3DT  
SW 79DT                   9DT  

I want to  get a new variable  t in which I want to replace the value from UniqueID in oldstring
so that I get the following:
SW7  
SW7  
SW 7

I tried the normal replace function but can I use placeholders there? So that I do not search for a specific word in the syntax but for placeholder UniqueID? Because I do not know the value I want to search for beforehand and besides that it differs from case to case.


compute t = replace (oldstring,'#UniqueID#',"AA").

Thank you!
 
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

Andy W
Your so close! You can pass the variable name in the REPLACE function, the second argument does not need to be a quoted string.

compute t = replace (oldstring,UniqueID,"").
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

emma78
Thank you!
I tried this but as a result I get the same text in t as in UniqueID.


SW72DT   2DT   SW72DT    
SW7 2DT 2DT   SW7 2DT  
SW 9 5DT 5DT   SW 9 5DT  


What am I doing wrong:-)?
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

Andy W
I will hazard my best guess - the UniqueID field has padded characters. You can fix this by using RTRIM - see example below.

*************************************************.
DATA LIST FREE /oldstring UniqueID (2A10).
BEGIN DATA
"SW72DT" 2DT  
"SW7 3DT" 3DT  
"SW 79DT" 9DT  
END DATA.

STRING new1 new2 (A10).
COMPUTE new1 = REPLACE(oldstring,UniqueID,"").
COMPUTE new2 = REPLACE(oldstring,RTRIM(UniqueID),"").
*************************************************.

If that is not it, you will need to provide an example dataset to give any better advice.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

emma78
Cool, thank you:-)
Reply | Threaded
Open this post in threaded view
|

Re: Search and replace

David Marso
Administrator
In reply to this post by Andy W
If the part to be replaced is always at the end of the text you could use CHAR.SUBSTR with CHAR.RINDEX.
This might be useful in cases where the 'UniqueID' might also occur in the front part of the 'oldstring'.
Defensive programming...CYA
--------------------------------
DATA LIST FREE /oldstring UniqueID (2A10).
BEGIN DATA
"SW72DT" 2DT  
"SW7 3DT" 3DT  
"SW 79DT" 9DT  
END DATA.
STRING new (A10).
COMPUTE new=CHAR.SUBSTR(oldstring,1,CHAR.RINDEX(oldstring,RTRIM(UniqueID))-1).
LIST.
 
oldstring  UniqueID   new
 
SW72DT     2DT        SW7
SW7 3DT    3DT        SW7
SW 79DT    9DT        SW 7
 
 
Number of cases read:  3    Number of cases listed:  3
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
Here's syntax showing both Andy's and David's methods with a 4th case included in the sample data that demonstrates the difference between them.  In the 4th case, note that the string 2DT occurs twice, and the question is how do you want to handle that case?  Variable new2 (from Andy) does it one way, and new3 (from David) another.

DATA LIST FREE /oldstring UniqueID (2A10).
BEGIN DATA
"SW72DT" 2DT  
"SW7 3DT" 3DT  
"SW 79DT" 9DT  
"SW72DT 2DT" 2DT
END DATA.

STRING new1 new2 new3 (A10).
* new1 and new2 are computed using Andy's syntax.
COMPUTE new1 = REPLACE(oldstring,UniqueID,"").
COMPUTE new2 = REPLACE(oldstring,RTRIM(UniqueID),"").
* new3 is computed using David's syntax.
COMPUTE new3=CHAR.SUBSTR(oldstring,1,CHAR.RINDEX(oldstring,RTRIM(UniqueID))-1).
LIST.
* Note that new2 and new3 give different results for the last case.

HTH.


David Marso wrote
If the part to be replaced is always at the end of the text you could use CHAR.SUBSTR with CHAR.RINDEX.
This might be useful in cases where the 'UniqueID' might also occur in the front part of the 'oldstring'.
Defensive programming...CYA
--------------------------------
DATA LIST FREE /oldstring UniqueID (2A10).
BEGIN DATA
"SW72DT" 2DT  
"SW7 3DT" 3DT  
"SW 79DT" 9DT  
END DATA.
STRING new (A10).
COMPUTE new=CHAR.SUBSTR(oldstring,1,CHAR.RINDEX(oldstring,RTRIM(UniqueID))-1).
LIST.
 
oldstring  UniqueID   new
 
SW72DT     2DT        SW7
SW7 3DT    3DT        SW7
SW 79DT    9DT        SW 7
 
 
Number of cases read:  3    Number of cases listed:  3
--
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/).