Syntax for Proper Case instead of all Caps

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

Syntax for Proper Case instead of all Caps

meljr
Hi All, I have  name, address, and city variables in all upper case. I want them in "proper case", or with just the first letter in caps. Like DENVER would be Denver and MAIN STREET would be Main Street. I am finding an upper and lower case function, but can't seem to find anything on the "proper case". Sometimes I will put the data into excel and use their "proper case" function, but this is too large of a data set.

Any ideas or suggestions?

Thanks
meljr
Reply | Threaded
Open this post in threaded view
|

Re: Syntax for Proper Case instead of all Caps

Maguin, Eugene
Meijr,

As far as I know there is no function such as upcase that converts a text
value to proper case. However, I wouldn't be surprised if there is a
programmibility routine lurking on the spss website to do this. Perhaps the
programmibility guy from spss can comment. Another good place to look is the
data management and programming book/pdf published by Ray Levesque and also
available on the spss website (and referenced in your spss installation
directory if the right things were installed). That said, I'll assume that
you have a variable containing a single value rather than a string
containing several values (It's not clear from your message which might be
true). That is, 'PROPER' rather than 'PROPER CASE VALUE'. Let your variable
be v1 with length len.

Compute v1=concat(substr(v1,1,1),lower(substr(v1,2,len))).


Gene Maguin
Reply | Threaded
Open this post in threaded view
|

Re: Syntax for Proper Case instead of all Caps

Peck, Jon
What you are looking for is so-called titlecasing.  SPSS doesn't have a function for this, but here is a little Python program that does the job.  Explanation below the code.

* Put text in title case.
* Capitalize the first character of each word (blank separated)
and lower case the rest.

begin program.
import spss, trans

def titlecase(s):
    """return string s in title case"""
    return " ".join([token[0].upper() + token[1:].lower() for token in s.split()])

t= trans.Tfunction()
t.append(titlecase, "TitleCased", "A100", ["address"])
t.execute()
end program.

It is using the trans module available on www.spss.com/devcentral, which also requires spssaux.py, spssdata.py, and namedtuple.py and at least SPSS 15.

The function titlecase does the title casing.
It splits the string into a list of pieces based on the occurrence of white space (s.split()).
It takes the first character of each piece and upper cases it (token[0].upper().
It takes the rest of the piece and lower cases it (token[1:].lower()).
It joins all these pieces back together with
" ".join(...).

The rest of the code just applies this function to the variable "address", creating a new variable named "TitleCased", which is a string of size 100 ("A100").  That could all be packaged and parameterized, but this example shows how to do this task.

HTH,
Jon Peck


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Gene Maguin
Sent: Thursday, April 26, 2007 3:06 PM
To: [hidden email]
Subject: Re: [SPSSX-L] Syntax for Proper Case instead of all Caps

Meijr,

As far as I know there is no function such as upcase that converts a text
value to proper case. However, I wouldn't be surprised if there is a
programmibility routine lurking on the spss website to do this. Perhaps the
programmibility guy from spss can comment. Another good place to look is the
data management and programming book/pdf published by Ray Levesque and also
available on the spss website (and referenced in your spss installation
directory if the right things were installed). That said, I'll assume that
you have a variable containing a single value rather than a string
containing several values (It's not clear from your message which might be
true). That is, 'PROPER' rather than 'PROPER CASE VALUE'. Let your variable
be v1 with length len.

Compute v1=concat(substr(v1,1,1),lower(substr(v1,2,len))).


Gene Maguin