removing final punctuation in a string

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

removing final punctuation in a string

Cleland, Patricia (EDU)

A colleague is analyzing a string variable.  The same phrases appear in a number of responses. She wants to aggregate those responses but some respondents ended their response with a period and others didn’t. This means that AGGREGATE treats them as different responses.

 

Is there a way to remove the period at the end of the response only?  (She can remove all the periods, but that causes other problems.)

 

TIA

 

Pat

 

 

Reply | Threaded
Open this post in threaded view
|

Re: removing final punctuation in a string

Maguin, Eugene
Patricia,
 
Rtrim. Look at this function.
 
Gene Maguin


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Cleland, Patricia (EDU)
Sent: Tuesday, December 07, 2010 9:04 AM
To: [hidden email]
Subject: removing final punctuation in a string

A colleague is analyzing a string variable.  The same phrases appear in a number of responses. She wants to aggregate those responses but some respondents ended their response with a period and others didn’t. This means that AGGREGATE treats them as different responses.

 

Is there a way to remove the period at the end of the response only?  (She can remove all the periods, but that causes other problems.)

 

TIA

 

Pat

 

 

Reply | Threaded
Open this post in threaded view
|

Re: removing final punctuation in a string

Art Kendall
In reply to this post by Cleland, Patricia (EDU)
The example syntax below is verbose for readability. It can be done more compactly.  It assumes that "same" should be taken with a grain of salt.

Copy the example below into a new instance of SPSS. See if this does what you want. She can aggregate on newvar2 or finalstring.

Art Kendall
Social Research Consultants

data list list /oldstring (a30).
begin data
"123 A St. someplace."
"123 A St. someplace"
"123 A St. Someplace."
"123 A St. some place"
"27.5 oz."
"27.5 oz"
end data.
string  newstring(a30).
*the inner rtrim removes trailing blanks.
*the outer rtrim removes a trailing period.
compute newstring= Rtrim(rtrim(oldstring),'.').
autorecode variables = newstring /into newvar1/print.
*the recode depends on what is on the output file.
recode newvar1 (1 thru 3 =1)(else=copy)into newvar2 .
formats newvar2 (f2).
string finalstring(a30).
APPLY DICTIONARY from * /SOURCE VARIABLES = newvar1 /TARGET VARIABLES = newvar2.
compute finalstring = VALUELABEL(newvar2).
list.



On 12/7/2010 9:04 AM, Cleland, Patricia (EDU) wrote:

A colleague is analyzing a string variable.  The same phrases appear in a number of responses. She wants to aggregate those responses but some respondents ended their response with a period and others didn’t. This means that AGGREGATE treats them as different responses.

 

Is there a way to remove the period at the end of the response only?  (She can remove all the periods, but that causes other problems.)

 

TIA

 

Pat

 

 

===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: removing final punctuation in a string

Cleland, Patricia (EDU)

Thanks Art. That worked great.

 

However, after she ran it my colleague discovered that some respondents had ended their responses with other punctuation marks such as colon, semi-colon or question mark. Is there a way to trim all of these on a single pass or will she need to re-run the code 4 times, first trimming out the periods, then the colons, then the semi-colons and finally the question marks?

 

Pat


From: Art Kendall [mailto:[hidden email]]
Sent: December 7, 2010 10:18 AM
To: Cleland, Patricia (EDU)
Cc: [hidden email]
Subject: Re: [SPSSX-L] removing final punctuation in a string

 

The example syntax below is verbose for readability. It can be done more compactly.  It assumes that "same" should be taken with a grain of salt.

Copy the example below into a new instance of SPSS. See if this does what you want. She can aggregate on newvar2 or finalstring.

Art Kendall
Social Research Consultants

data list list /oldstring (a30).
begin data
"123 A St. someplace."
"123 A St. someplace"
"123 A St. Someplace."
"123 A St. some place"
"27.5 oz."
"27.5 oz"
end data.
string  newstring(a30).
*the inner rtrim removes trailing blanks.
*the outer rtrim removes a trailing period.
compute newstring= Rtrim(rtrim(oldstring),'.').
autorecode variables = newstring /into newvar1/print.
*the recode depends on what is on the output file.
recode newvar1 (1 thru 3 =1)(else=copy)into newvar2 .
formats newvar2 (f2).
string finalstring(a30).
APPLY DICTIONARY from * /SOURCE VARIABLES = newvar1 /TARGET VARIABLES = newvar2.
compute finalstring = VALUELABEL(newvar2).
list.



On 12/7/2010 9:04 AM, Cleland, Patricia (EDU) wrote:

A colleague is analyzing a string variable.  The same phrases appear in a number of responses. She wants to aggregate those responses but some respondents ended their response with a period and others didn’t. This means that AGGREGATE treats them as different responses.

 

Is there a way to remove the period at the end of the response only?  (She can remove all the periods, but that causes other problems.)

 

TIA

 

Pat

 

 

Reply | Threaded
Open this post in threaded view
|

Re: removing final punctuation in a string

Art Kendall
Assuming that there are not multiple punctuation marks at the end of the string just put in
compute newstring= Rtrim(rtrim(oldstring),':').
compute newstring= Rtrim(rtrim(oldstring),';').
compute newstring= Rtrim(rtrim(oldstring),'?').


Art Kendall
Social Research Consultants

On 12/7/2010 11:12 AM, Cleland, Patricia (EDU) wrote:

Thanks Art. That worked great.

 

However, after she ran it my colleague discovered that some respondents had ended their responses with other punctuation marks such as colon, semi-colon or question mark. Is there a way to trim all of these on a single pass or will she need to re-run the code 4 times, first trimming out the periods, then the colons, then the semi-colons and finally the question marks?

 

Pat


From: Art Kendall [[hidden email]]
Sent: December 7, 2010 10:18 AM
To: Cleland, Patricia (EDU)
Cc: [hidden email]
Subject: Re: [SPSSX-L] removing final punctuation in a string

 

The example syntax below is verbose for readability. It can be done more compactly.  It assumes that "same" should be taken with a grain of salt.

Copy the example below into a new instance of SPSS. See if this does what you want. She can aggregate on newvar2 or finalstring.

Art Kendall
Social Research Consultants

data list list /oldstring (a30).
begin data
"123 A St. someplace."
"123 A St. someplace"
"123 A St. Someplace."
"123 A St. some place"
"27.5 oz."
"27.5 oz"
end data.
string  newstring(a30).
*the inner rtrim removes trailing blanks.
*the outer rtrim removes a trailing period.
compute newstring= Rtrim(rtrim(oldstring),'.').
autorecode variables = newstring /into newvar1/print.
*the recode depends on what is on the output file.
recode newvar1 (1 thru 3 =1)(else=copy)into newvar2 .
formats newvar2 (f2).
string finalstring(a30).
APPLY DICTIONARY from * /SOURCE VARIABLES = newvar1 /TARGET VARIABLES = newvar2.
compute finalstring = VALUELABEL(newvar2).
list.



On 12/7/2010 9:04 AM, Cleland, Patricia (EDU) wrote:

A colleague is analyzing a string variable.  The same phrases appear in a number of responses. She wants to aggregate those responses but some respondents ended their response with a period and others didn’t. This means that AGGREGATE treats them as different responses.

 

Is there a way to remove the period at the end of the response only?  (She can remove all the periods, but that causes other problems.)

 

TIA

 

Pat

 

 

===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: removing final punctuation in a string

Albert-Jan Roskam
Hello!

Untested:

import spss, re
def stripPunctuation(targetVar):
    cur=spss.Cursor(accessType='w')
    for i in range(cur.GetCaseCount()):
        case = cur.fetchone()[0]
        cur.SetValueChar(targetVar, re.sub("\W+", "", case))
        cur.CommitCase()
    cur.close()

stripPunctuation(targetVar = "yourVarNameHere")
 
Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: Art Kendall <[hidden email]>
To: [hidden email]
Sent: Tue, December 7, 2010 5:31:23 PM
Subject: Re: [SPSSX-L] removing final punctuation in a string

Assuming that there are not multiple punctuation marks at the end of the string just put in
compute newstring= Rtrim(rtrim(oldstring),':').
compute newstring= Rtrim(rtrim(oldstring),';').
compute newstring= Rtrim(rtrim(oldstring),'?').


Art Kendall
Social Research Consultants

On 12/7/2010 11:12 AM, Cleland, Patricia (EDU) wrote:

Thanks Art. That worked great.

 

However, after she ran it my colleague discovered that some respondents had ended their responses with other punctuation marks such as colon, semi-colon or question mark. Is there a way to trim all of these on a single pass or will she need to re-run the code 4 times, first trimming out the periods, then the colons, then the semi-colons and finally the question marks?

 

Pat


From: Art Kendall [[hidden email]]
Sent: December 7, 2010 10:18 AM
To: Cleland, Patricia (EDU)
Cc: [hidden email]
Subject: Re: [SPSSX-L] removing final punctuation in a string

 

The example syntax below is verbose for readability. It can be done more compactly.  It assumes that "same" should be taken with a grain of salt.

Copy the example below into a new instance of SPSS. See if this does what you want. She can aggregate on newvar2 or finalstring.

Art Kendall
Social Research Consultants

data list list /oldstring (a30).
begin data
"123 A St. someplace."
"123 A St. someplace"
" 123 A St. Someplace."
"123 A St. some place"
"27.5 oz."
"27.5 oz"
end data.
string  newstring(a30).
*the inner rtrim removes trailing blanks.
*the outer rtrim removes a trailing period.
compute newstring= Rtrim(rtrim(oldstring),'.').
autorecode variables = newstring /into newvar1/print.
*the recode depends on what is on the output file.
recode newvar1 (1 thru 3 =1)(else=copy)into newvar2 .
formats newvar2 (f2).
string finalstring(a30).
APPLY DICTIONARY from * /SOURCE VARIABLES = newvar1 /TARGET VARIABLES = newvar2.
compute finalstring = VALUELABEL(newvar2).
list.



On 12/7/2010 9:04 AM, Cleland, Patricia (EDU) wrote:

A colleague is analyzing a string variable.  The same phrases appear in a number of responses. She wants to aggregate those responses but some respondents ended their response with a period and others didn’t. This means that AGGREGATE treats them as different responses.

 

Is there a way to remove the period at the end of the response only?  (She can remove all the periods, but that causes other problems.)

 

TIA

 

Pat

 

 

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