1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
* run the syntax to see what it does.
* Syntax will not work if the family name has a space in it.


* Posted to SPSSX-L list by Ray on 2002/04/09.
* http://pages.infinit.net/rlevesqu/index.htm

DATA LIST FIXED /names 1-50(a).
BEGIN DATA
A B & C D SMITH
A B SMITH & C D BLOGGS
A SMITH & C BLOGGS
A B SMITH & C BLOGGS
A B SMITH & C D BLOGGS & E F JONES
END DATA.
LIST.

STRING str1 TO str3(A20) /lastnam1 to lastnam3(A20).
COMPUTE str1=SUBSTR(names,1,INDEX(names,'&')-1).
DO IF INDEX(names,'&')=RINDEX(names,'&').
COMPUTE str2=SUBSTR(names,RINDEX(names,'&')+1).
ELSE.
COMPUTE str2=SUBSTR(names,INDEX(names,'&')+1,RINDEX(names,'&')-INDEX(names,'&')-1).
COMPUTE str3=SUBSTR(names,RINDEX(names,'&')+1).
END IF.

* Find family names.
DO REPEAT varstr=str1 str2 str3 /varlast=lastnam1 lastnam2 lastnam3.
IF LEN(RTRIM(varstr))>3 varlast=SUBSTR(varstr,RINDEX(RTRIM(varstr),' ')).
END REPEAT PRINT.
EXECUTE.

* Start to create the result tring.
STRING result(A50).
DO IF LEN(RTRIM(lastnam1))=0.
COMPUTE result=CONCAT(
	RTRIM(lastnam2),' ',RTRIM(str1),' AND',
	SUBSTR(str2,1,RINDEX(RTRIM(str2),' '))
	).
ELSE.
COMPUTE result=CONCAT(
	RTRIM(lastnam1),' ',
	SUBSTR(str1,1,RINDEX(RTRIM(str1),' ')),' AND',
	RTRIM(lastnam2),' ',
	SUBSTR(str2,1,RINDEX(RTRIM(str2),' '))
	).

END IF.
EXECUTE.

DO IF LEN(RTRIM(lastnam3))>0.
COMPUTE result=CONCAT(
	RTRIM(result),' AND',RTRIM(lastnam3),
	SUBSTR(str3,1,RINDEX(RTRIM(str3),' '))
	).
END IF.
EXECUTE.