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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
I have two SPSS files, T1.sav and T2.sav as follows:

T1 cases:
ID   V11
---  --
123  11
123  12

T2 cases:
ID   V21
---  --
123  21
123  22
123  23

I need to generate a file T3 containing the merged variables from T1 and T2.
The result I wish to get is as follows:
T3 cases:
ID  V11 V21
--- --  --
123 11  21
123 11  22
123 11  23
123 12  21
123 12  22
123 12  23

*** Solution posted by Raynald Levesque to SPSSX-L on 2001\\05\\04.

*The following solution assumes the variables of interest are numeric. (It
*would not be difficult to modify it to work with strings if necessary).

*The solution is fairly general in the sense that it works for any give
*number of values of id, v11 and v12.

*I have however assumed that there is a maximum of 50 different values. You
*have to replace 50 in the syntax by a larger number if applicable. It would
*be possible to make it fully automated and have the syntax calculate that
*number but I did not bother for now.

DATA LIST LIST /id v11.
BEGIN DATA
123  11
123  12
124  15
124  16
124  18
124  19
END DATA.
LIST.
SAVE OUTFILE='c:\\temp\\t1.sav'.

DATA LIST LIST /id v21.
BEGIN DATA
123  21
123  22
123  23
124  26
124  27
END DATA.
LIST.
SAVE OUTFILE='c:\\temp\\t2.sav'.

*Reformat first file.
GET FILE='c:\\temp\\t1.sav'.
COMPUTE caseid=$CASENUM.
RANK VARIABLES=caseid  (A) BY id /RANK INTO idx.
VECTOR v11_(50F8.0).
COMPUTE v11_(idx)=v11.
AGGREGATE
  /OUTFILE=*
  /BREAK=id
  /v11_1 TO v11_50 = MAX(v11_1 TO v11_50).
SAVE OUTFILE='c:\\temp\\t11.sav'.

*Reformat first file.
GET FILE='c:\\temp\\t2.sav'.
COMPUTE caseid=$CASENUM.
RANK VARIABLES=caseid  (A) BY id /RANK INTO idx.
VECTOR v21_(50F8.0).
COMPUTE v21_(idx)=v21.
AGGREGATE
  /OUTFILE=*
  /BREAK=id
  /v21_1 TO v21_50 = MAX(v21_1 TO v21_50).

* Combine the 2 files.

MATCH FILES /FILE=*
 /FILE='C:\\Temp\\t11.sav'
 /BY id.

* Rearrange the file.
VECTOR v21_=v21_1 TO v21_50 /v11_=v11_1 TO v11_50.
LOOP cnt1=1 TO 50.
COMPUTE v11=v11_(cnt1).
LOOP cnt2=1 TO 50.
COMPUTE v21=v21_(cnt2).
XSAVE OUTFILE='c:\\temp\\targetfile.sav' /KEEP=id v11 v21.
END LOOP IF MISSING(v21).
END LOOP IF MISSING(v11).
EXECUTE.
GET FILE='c:\\temp\\targetfile.sav'.
SELECT IF ~MISSING(v11)&~MISSING(v21).
EXECUTE.