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
*(Q) How can I transform my variable "a" so that it only takes on values in the interval [0,1].

*(A) Following linear transformation was posted by rlevesque@videotron.ca to SPSSX-L on 2001/09/25.

DATA LIST FREE /a.
BEGIN DATA
-5 12 85 26 -2 18
END DATA.
LIST.

COMPUTE dummy=1.
AGGREGATE
  /OUTFILE='C:\\temp\\AGGR.SAV'
  /BREAK=dummy
  /minval = MIN(a) /maxval = MAX(a).

MATCH FILES /FILE=*
 /TABLE='C:\\Temp\\AGGR.SAV'
 /BY dummy.

COMPUTE newa=(a-minval)/(maxval - minval).


************************************.
*
* Extension 1.
* To turn the above into a macro. Do as follows.
*
************************************.

SET MPRINT=no.
*/////////////////////.
DEFINE !transf (var=!TOKENS(1) /newvar=!TOKENS(1))
COMPUTE dummy=1.
AGGREGATE
  /OUTFILE='C:\\temp\\AGGR.SAV'
  /BREAK=dummy
  /minval = MIN(!var) /maxval = MAX(!var).

MATCH FILES /FILE=*
 /TABLE='C:\\Temp\\AGGR.SAV'
 /BY dummy.

COMPUTE !newvar=(!var-minval)/(maxval - minval).
!ENDDEFINE.
*/////////////////////.

* Example of use.
SET MPRINT=yes.
GET FILE='c:\\Program Files\\spss\\employee data.sav'.
!transf var=salary newvar=nsal.
EXECUTE.


************************************.
*
* Extension 2.
* To allow transformation into any range [b,c] instead of between [0,1]. Do as follows.
*
************************************.

SET MPRINT=no.
*/////////////////////.
DEFINE !transf2 (var=!TOKENS(1) 
	/newvar=!TOKENS(1)
	/intbeg=!TOKENS(1)
	/intend=!TOKENS(1))

/* Transform var into newvar such that newvar is in interval [intbeg,intend]*/

COMPUTE dummy=1.
AGGREGATE
  /OUTFILE='C:\\temp\\AGGR.SAV'
  /BREAK=dummy
  /minval = MIN(!var) /maxval = MAX(!var).

MATCH FILES /FILE=*
 /TABLE='C:\\Temp\\AGGR.SAV'
 /BY dummy.
COMPUTE !newvar=!intbeg + (!intend - !intbeg)*(!var-minval)/(maxval - minval).
!ENDDEFINE.
*/////////////////////.

* Example of use.
SET MPRINT=yes.
GET FILE='c:\\Program Files\\spss\\employee data.sav'.
!transf2 var=salary newvar=nsal intbeg=2 intend=7.
EXECUTE.
* Proof.
DESCRIPTIVES
  VARIABLES=nsal
  /STATISTICS=MEAN RANGE MIN MAX .