I have a column with names of different persons separated by comma, for example, (all in 1 cell) Ben Lee, Paul Loy, Boy Lim. I want to separate each name into different columns. How will I do it? (in SPSS syntax).
2 Answers
1
votes
See this thread with potential solution(s). Namely, credit David Marso and Jon Peck:
* General Parser *.
DATA LIST / X 1-80 (A).
BEGIN DATA 11-0101-423-7384
END DATA.
VECTOR NUMS(10).
COMPUTE #0=0.
LOOP.
COMPUTE #1=INDEX(X,'-').
COMPUTE #0=#0+1.
IF #1>0 NUMS(#0)=NUMBER(SUBSTR(X,1,#1-1),F8).
COMPUTE X=SUBSTR(X,#1+1).
END LOOP IF #1=0.
COMPUTE NUMS(#0)=NUMBER(X,F8).
MATCH FILES FILE * / DROP X.
LIST.
Or alternatively a python solution:
data list free /x(a13).
begin data.
1,13,5,6,99,8
end data.
dataset name data.
begin program.
def split(v):
return v.split(',')
end program.
spssinc trans result = v1 to v6
/formula "split(x)".
0
votes
do repeat TXTname="Ben Lee" "Paul Loy" "Boy Lim"/VRname=BenLee PaulLoy BoyLim.
compute VRname=index(OriginalColumnName, TXTname)>0.
end repeat.
If there are many more names you might prefer to use standard variable names instead, and add the actual names in the labels instead:
do repeat TXTname="Ben Lee" "Paul Loy" "Boy Lim"/VRname=Name01 to Name03.
compute VRname=index(OriginalColumnName, TXTname)>0.
end repeat.
variable labels
Name01 "Ben Lee"
Name02 "Paul Loy"
Name03 "Boy Lim".