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
* My file has many variables, some have the format aaaa0n where a is a letter an n a number.
* I need to rename these variables to the format aaarn.

* Solution by Ray.

DATA LIST /v1(F8.0).
BEGIN DATA.
1
END DATA.

NUMERIC vname01 TO vname09 vara varb varc test01 TO test09 (F8.0).
SAVE OUTFILE='c:\\temp\\mydata.sav'.
FLIP.

STRING nb1 nb2 (A1) newname(A8).
COMPUTE nbchar=LEN(RTRIM(case_lbl)).
COMPUTE nb1=SUBSTR(case_lbl,nbchar-1,1).
COMPUTE nb2=SUBSTR(case_lbl,nbchar).
DO IF nb1="0" and RANGE(nb2,"0","9").
COMPUTE newname=CONCAT(SUBSTR(case_lbl,1,nbchar-2),nb2,"r").
WRITE OUTFILE='c:\\temp\\rename.sps'
/ 'RENAME VARIABLE ('case_lbl'='newname').'.
END IF.
Execute.

GET FILE='c:\\temp\\mydata.sav'.
INCLUDE 'C:\\temp\\rename.sps'.