|
Does SPSS have a function similar to the "TRANWRD" function used in SAS?
The TRANWRD function is basically a search and replace function which searches a feild for a specified set of characters and replaces that set of characters with another specified set. Thanks in advance! Michael Bates ===================== 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 |
|
Administrator
|
REPLACE. REPLACE(a1, a2, a3[, a4]). String. In a1, instances of a2 are replaced with a3. The optional argument a4 specifies the number of occurrences to replace; if a4 is omitted, all occurrences are replaced. Arguments a1, a2, and a3 must resolve to string values (literal strings enclosed in quotes or string variables), and the optional argument a4 must resolve to a non-negative integer. For example, REPLACE("abcabc", "a", "x") returns a value of "xbcxbc" and REPLACE("abcabc", "a", "x", 1) returns a value of "xbcabc". Example from Command Syntax Reference: DATA LIST FREE / FullName (A20). BEGIN DATA "Fred Smith" END DATA. STRING FirstName LastName LastFirstName (A20). COMPUTE #spaceLoc=INDEX(FullName, " "). COMPUTE FirstName=SUBSTR(FullName, 1, (#spaceLoc-1)). COMPUTE LastName=SUBSTR(FullName, (#spaceLoc+1)). COMPUTE LastFirstName=CONCAT(RTRIM(LastName), ", ", FirstName). COMPUTE LastFirstName=REPLACE(LastFirstName, "Fred", "Ted"). EXECUTE.
--
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/). |
|
In reply to this post by Michael W. Bates
Will something like this do?
recode varname ("character field 1" = "character
field 2")
Might need to use INTO as SPSS sometimes won't
allow recodes into same variables
recode varname1 ("character field 1" = "character
field 2") INTO varname2
|
|
Hi,
This will replace substrings too: string newvar (a50). compute newvar = replace(oldvar, "oldstring", "newstring"). You should check that the newvar is wide enough (probably of equal width as the old variable). Cheers!! Albert-Jan --- On Tue, 9/29/09, John F Hall <[hidden email]> wrote: > From: John F Hall <[hidden email]> > Subject: Re: [SPSSX-L] Search and Replace Function > To: [hidden email] > Date: Tuesday, September 29, 2009, 11:17 PM > > > > > > > > Will something like this > do? > > recode varname > ("character field 1" = "character > field 2") > > Might need to use INTO as > SPSS sometimes won't > allow recodes into same variables > > > recode varname1 > ("character field 1" = "character > field 2") INTO varname2 > > ----- Original Message > ----- > From: > Michael W. > Bates > To: [hidden email] > > Sent: Tuesday, > September 29, 2009 9:17 > PM > Subject: Search and > Replace > Function > > > Does SPSS have a function similar to the > "TRANWRD" function > used in SAS? > > The TRANWRD function is basically a search and replace > function which > searches a feild for a specified set of characters and > replaces that set > of characters with another specified set. > > Thanks > in advance! > > Michael Bates > > ===================== > 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 |
|
In reply to this post by John F Hall
SPSS does not require the INTO. However, I don't even tell beginners about the RECODE without the INTO since usually it is not a good idea to write over an existing variable. That enhances the quality of the "audit trail" and makes it possible to do the inevitable re-draft of the whole process. Art Kendall Social Research Consultants John F Hall wrote: ===================== 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
Art Kendall
Social Research Consultants |
|
In reply to this post by Michael W. Bates
As has been pointed out, the REPLACE function replaces one string with another. That's part of the standard SPSS transformation language (as of version 14 or so).
There are also some other functions in the extendedTransforms module available through programmability that do related things. - The subs function replaces a pattern written as a regular expression with another expression. Very powerful, but you need to write regular expressions to use it. - The templatesub function is more flexible than REPLACE but does not require regular expressions - The translatechar function maps individual characters into other characters. All of these can be used with the SPSSINC TRANS extension command if you have version 17 or 18, which eliminates the need to write Python code other than the one line that calls the function. Regards, Jon Peck -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Michael W. Bates Sent: Tuesday, September 29, 2009 1:18 PM To: [hidden email] Subject: [SPSSX-L] Search and Replace Function Does SPSS have a function similar to the "TRANWRD" function used in SAS? The TRANWRD function is basically a search and replace function which searches a feild for a specified set of characters and replaces that set of characters with another specified set. Thanks in advance! Michael Bates ===================== 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 |
|
In reply to this post by Michael W. Bates
On Tue, 29 Sep 2009 15:17:47 -0400, Michael W. Bates <[hidden email]>
wrote: >Does SPSS have a function similar to the "TRANWRD" function used in SAS? > >The TRANWRD function is basically a search and replace function which >searches a feild for a specified set of characters and replaces that set >of characters with another specified set. Well, actually, you don't need a function for that! On Raynald's Levesques website I found the following very curious code COMPUTE SUBSTR(v,INDEX(v,"."),1)=",". It is curious, because in most programming languages the left part of an assignment (the part before the "="-sign) may only be a variable and functions like SUBSTR() may only be used in the other part. Nevertheless it works (at least in v17.02). I don't think SPSS has documented this feature (I may be wrong), so in future releases it may not work. See: http://www.spsstools.net/Macros/Tutorials/changeSomeCharactersInsideManyStr ingVariables.txt Antoon Smulders ===================== 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 |
|
Yes, substr usage on the left-hand side is both unusual and documented. However, that feature cannot be used in Unicode mode due to the varying size of characters. The REPLACE function handles the general case as well as being more straightforward to use.
The code below, of course, replaces only the first instance of the character. Regards, Jon Peck -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Antoon Smulders Sent: Thursday, October 01, 2009 1:41 AM To: [hidden email] Subject: Re: [SPSSX-L] Search and Replace Function On Tue, 29 Sep 2009 15:17:47 -0400, Michael W. Bates <[hidden email]> wrote: >Does SPSS have a function similar to the "TRANWRD" function used in SAS? > >The TRANWRD function is basically a search and replace function which >searches a feild for a specified set of characters and replaces that set >of characters with another specified set. Well, actually, you don't need a function for that! On Raynald's Levesques website I found the following very curious code COMPUTE SUBSTR(v,INDEX(v,"."),1)=",". It is curious, because in most programming languages the left part of an assignment (the part before the "="-sign) may only be a variable and functions like SUBSTR() may only be used in the other part. Nevertheless it works (at least in v17.02). I don't think SPSS has documented this feature (I may be wrong), so in future releases it may not work. See: http://www.spsstools.net/Macros/Tutorials/changeSomeCharactersInsideManyStr ingVariables.txt Antoon Smulders ===================== 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 |
| Free forum by Nabble | Edit this page |
