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:
data list /stname(a30).
Johnson Bob R.
Johnson Robert R.
string dupname (a30).
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).
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.
16 June 2018