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
*(Q) How can I winsorize a mean?

*(A) Raynald Levesque 2002/07/17
	http://pages.infinit.net/rlevesqu/index.htm.

*//////////////.
DEFINE !winsor (var=!TOKENS(1) /lowpc=!TOKENS(1) 
		/hipc=!TOKENS(1) /newvar=!TOKENS(1))

COMPUTE nobreak=1.
RANK VARIABLES=!var (A) BY nobreak  /PERCENT INTO pc_ /PRINT=YES
  /TIES=MEAN .
XSAVE OUTFILE='c:\\temp\\data.sav'.

SORT CASES BY pc_ (D).
SELECT IF pc_ <= !lowpc.
N OF CASES 1.
RENAME VARIABLE (!var=minval).
SAVE OUTFILE='c:\\temp\\minval.sav' /KEEP=nobreak minval.

GET FILE='c:\\temp\\data.sav'.
SELECT IF pc_ >= (100 - !hipc).
SORT CASES BY pc_ (A).
N OF CASES 1.
RENAME VARIABLE (!var=maxval).
SAVE OUTFILE='c:\\temp\\maxval.sav' /KEEP=nobreak maxval.

MATCH FILES FILE='c:\\temp\\data.sav'
		/TABLE='c:\\temp\\minval.sav'
		/TABLE='c:\\temp\\maxval.sav'
		/BY=nobreak
		/DROP=nobreak.
COMPUTE !newvar=MAX(MIN(!var,maxval),minval).
EXECUTE.
!ENDDEFINE.
*//////////////.


* Example of use.
GET FILE='c:\\program files\\spss\\employee data.sav'.

SET MPRINT=yes.
* Next command creates variable winsal where 
*	lower 5% of salaries are replaced by the next higher salary.
*	higher 10% of salaries are replaced by the next lower salary.
!winsor var=salary lowpc=5 hipc=10 newvar=winsal.
SET MPRINT=no.


SUMMARIZE
  /TABLES=salary winsal
  /FORMAT=NOLIST TOTAL
  /TITLE='Case Summaries'
  /MISSING=VARIABLE
  /CELLS=COUNT MEAN .