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
* (Q) I have 2 data files with the same variables, in the same order but variable names are different. 
*  How can I automatically rename variables of file 1 to the names contained in file 2?

* (A) Posted to SPSSX-L list on 2003/09/05 by Raynald Levesque.


DATA LIST LIST /var1 var2 var3.
BEGIN DATA
1 2 3 
END DATA.
SAVE OUTFILE='c:\\temp\\old names.sav'.

DATA LIST LIST /x y z.
BEGIN DATA
1 2 3 
END DATA.
SAVE OUTFILE='c:\\temp\\new names.sav'.

******* Start job *********.

*///////////////////.
DEFINE !vname(old=!TOKENS(1) /new=!TOKENS(1))
GET FILE=!new.
N OF CASES 1.
FLIP.
COMPUTE nb=$CASENUM.
SAVE OUTFILE='c:\\temp\\new names2.sav' /RENAME=(case_lbl=newnames).

GET FILE=!old.
N OF CASES 1.
FLIP.
RENAME VARIABLES (case_lbl=oldnames).
COMPUTE nb=$CASENUM.
MATCH FILES FILE=* 
	/FILE='c:\\temp\\new names2.sav'.
WRITE OUTFILE='c:\\temp\\rename syntax.sps'
	/"RENAME VARIABLES ("newnames"="oldnames").".
EXECUTE.

GET FILE=!new.
INCLUDE 'c:\\temp\\rename syntax.sps'.
!ENDDEFINE.
*///////////////////.

SET MPRINT=yes.
!vname old='c:\\temp\\old names.sav' 
	new='c:\\temp\\new names.sav'.
SET MPRINT=no.