IBM Support

Stripping apart first name, middle initial, last name from a string variable

Troubleshooting


Problem

I have student names specifically in the form last name first name middle initial (i.e., Johnson Bob R.) where the middle initial is optional. I want to separate these into 3 variables. How can I do this?

Resolving The Problem

This example will ry this syntax:

*Sample data
data list /stname(a30).
begin data
Johnson Bob
Johnson Robert
Johnson Bob R.
Johnson Robert R.
end data.

*Syntax job.
string dupname (a30).
compute dupname=stname.
execute.

string lname (a10).
string fname (a10).
string midinit (a2).
string mi (a2).
compute lname=substr(dupname,1,index(dupname," ")-1).
compute dupname=substr(dupname,index(dupname," ")+1).
compute fname=substr(dupname,1,index(dupname," ")-1).
compute midinit=substr(dupname,index(dupname," ")+1).
execute.
compute mi=rtrim(midinit).
execute.

This syntax creates three variables: LNAME, FNAME, and MI. SPSS then parses out the last name value, takes that value out of the DUPNAME variable, and places it in the LNAME variable. Then, it takes the first name value, pulls that
out, and places it in the FNAME variable. This leaves the middle initial (if there is one). That gets placed into the MI variable.

(The DUPNAME variable is created to leave the original STNAME variable intact. When we get done with the DUPNAME variable, the last name will be gone.

[{"Product":{"code":"SSLVMB","label":"SPSS Statistics"},"Business Unit":{"code":"BU001","label":"Analytics Private Cloud"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"Not Applicable","Edition":""}]

Historical Number

14533

Document Information

Modified date:
16 June 2018

UID

swg21476068