Substr

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

Substr

JKRockStomper
Hi,

I have a string variable name (a100) which contains first, middle, and last names.

I would like to only keep the last name of this variable, but I am not sure how to do this because not always is there a middle name.  If there is not a middle name there are two spaces separating the first and middle names.

Joe D Public
John Smith
Kate H Handle


Thanks,

Reply | Threaded
Open this post in threaded view
|

Re: Substr

Jon K Peck
data list fixed /name(a20).
begin data
Joe D Public
John Smith
Kate H Handle
end data.
dataset name data.

string lastname(a10).
compute lastname = substr(name, rindex(name, " ")+1).
list.
exec.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
312-651-3435




From:        JKRockStomper <[hidden email]>
To:        [hidden email]
Date:        01/13/2011 03:20 PM
Subject:        [SPSSX-L] Substr
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi,

I have a string variable name (a100) which contains first, middle, and last
names.

I would like to only keep the last name of this variable, but I am not sure
how to do this because not always is there a middle name.  If there is not a
middle name there are two spaces separating the first and middle names.

Joe D Public
John Smith
Kate H Handle


Thanks,


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Substr-tp3340517p3340517.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

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

Rick Oliver-3
In reply to this post by JKRockStomper
use char.rindex to find the first space going right to left.

not tested:
string newname (a100).
compute #x=char.rindex(oldname, " ")+1.
compute newname=substr(oldname, #x).

With older versions, use rindex instead of char.rindex




From:        JKRockStomper <[hidden email]>
To:        [hidden email]
Date:        01/13/2011 04:14 PM
Subject:        Substr
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi,

I have a string variable name (a100) which contains first, middle, and last
names.

I would like to only keep the last name of this variable, but I am not sure
how to do this because not always is there a middle name.  If there is not a
middle name there are two spaces separating the first and middle names.

Joe D Public
John Smith
Kate H Handle


Thanks,


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Substr-tp3340517p3340517.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

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