Date: Thu, 24 Jul 2003 11:34:10 -0500
Reply-To: "Marks, Jim" <Jim.Marks@lodgenet.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: "Marks, Jim" <Jim.Marks@lodgenet.com>
Subject: Re: replacing parts of a string variable
Content-Type: text/plain; charset="iso-8859-1"
This will work:
DATA LIST /strvar (A40).
Gonzales Middle School
School of Hard Knocks
University of Waterloo
Middle School of Schools
STRING newstr (A40).
DO IF INDEX(strvar,'School') GT 0.
COMPUTE #place EQ INDEX(strvar,'School').
COMPUTE #lngth EQ LENGTH(RTRIM(strvar)).
COMPUTE newstr EQ CONCAT(SUBSTR(strvar,1,#place-1),'Sch',SUBSTR(strvar,#place+6, #lngth-#place+6)).
COMPUTE newstr EQ strvar.
Note case 3-- "school" is not changed.
Note case 4-- the second instance of "School" is not affected.
Note case 5-- "Schools" is changed to "Schs".
For other words, don't forget to change the number in the second SUBSTR in the final COMPUTE-- "School" has 6 characters.
I prefer to create new variables rather than change existing variables. Mistakes are easier to fix that way :-)
Senior Market Analyst
LodgeNet Entertainment Corporation
From: John Diez - Network & Online Services [mailto:JDiez@rnchq.org]
Sent: Thursday, July 24, 2003 10:21 AM
Subject: replacing parts of a string variable
i have a string variable that has recurring words in it that i would like to truncate. how can you replace part of a string? for example, i would like every occurrence of "school" to be truncated to "sch", so the value "Gonzales Middle School" would be "Gonzales Middle Sch". is there a find and replace command?