pulling apart string variables

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

pulling apart string variables

Nancy Rusinak-2
Hello All,

I have a data set with a variable called FullName (A43) that contains data in the following format:

Roberts, Nancy A.
Smith, John Doe
ZAMARAIS, BILLY B
Cullen, Martha Ann
HENDERSON-RAMERIZ, BONNIE K.

My end goal is to get the uppercase data in the same format as the rest of the data which I believe is called titlecasing (i.e. first letter of proper names capitalized, rest lower case).  I'm running V22, which means I have python and I found a nifty program online that should work, but it references something called a trans module, which I do not seem to have and will not be allowed to download.  But the link is almost 8 years old so perhaps things have changed.  Let me know if there is a python program that might help.

In the meantime, I think I will have to create substrings, manipulate the lettercase and then concatenate everything back into one field.  I have syntax that puts the last name in its own field by pulling everything prior to the comma.  I am having difficulty splitting out first name and middle name into separate fields, however, since they are delimited by spaces instead of commas.

I am also not sure how I will handle creating uppercase letters for last names where a hypen is present, such as HENDERSON-RAMERIZ.

Would love some help here from others who have tackled this before me.  Thanks, in advance, for any assistance that can be offered.

Nancy

=====================
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: pulling apart string variables

Jon K Peck
While you could do this with traditional syntax, here's the programmability solution.

Since you have V22 with Python, you already have the SPSSINC TRANS extension command, which is the descendant of the old trans module.

This code will do the job.

begin program.
def title(x):
    return x.title()
end program.

spssinc trans result=titlecased type=32
/formula "title(FullName)".




Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Nancy <[hidden email]>
To:        [hidden email]
Date:        02/18/2015 10:32 AM
Subject:        [SPSSX-L] pulling apart string variables
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hello All,

I have a data set with a variable called FullName (A43) that contains data in the following format:

Roberts, Nancy A.
Smith, John Doe
ZAMARAIS, BILLY B
Cullen, Martha Ann
HENDERSON-RAMERIZ, BONNIE K.

My end goal is to get the uppercase data in the same format as the rest of the data which I believe is called titlecasing (i.e. first letter of proper names capitalized, rest lower case).  I'm running V22, which means I have python and I found a nifty program online that should work, but it references something called a trans module, which I do not seem to have and will not be allowed to download.  But the link is almost 8 years old so perhaps things have changed.  Let me know if there is a python program that might help.

In the meantime, I think I will have to create substrings, manipulate the lettercase and then concatenate everything back into one field.  I have syntax that puts the last name in its own field by pulling everything prior to the comma.  I am having difficulty splitting out first name and middle name into separate fields, however, since they are delimited by spaces instead of commas.

I am also not sure how I will handle creating uppercase letters for last names where a hypen is present, such as HENDERSON-RAMERIZ.

Would love some help here from others who have tackled this before me.  Thanks, in advance, for any assistance that can be offered.

Nancy

=====================
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: pulling apart string variables

Andy W
In reply to this post by Nancy Rusinak-2
Python has a title case modifier to string objects - no external library needed.

**********************************.
DATA LIST FREE / MyStr (A100).
BEGIN DATA
"Roberts, Nancy A."
"Smith, John Doe"
"ZAMARAIS, BILLY B"
"Cullen, Martha Ann"
"HENDERSON-RAMERIZ, BONNIE K."
END DATA.

BEGIN PROGRAM.
def Title(s):
  return s.title()
END PROGRAM.

SPSSINC TRANS RESULT=TitleStr TYPE=100
/FORMULA Title(MyStr).

LIST MyStr TitleStr.
**********************************.

See this Q/A for more examples of splitting the string apart in SPSS, http://spssx-discussion.1045642.n5.nabble.com/How-to-transform-alpha-numerical-values-in-a-variable-in-lower-case-letters-so-that-the-first-letter-td5724418.html#a5724425, but here the python solution is pretty simple.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/