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
* QUESTION in SPSS newsgroup 2001/03/17.
*Does anyone know how to get SPSS to sort the variables in an SPSS
*FREQUENCIES (output) table?  In some procedures, such as DESCRIPTIVE,
*you can write this directly into the command syntax -- but I don't see
*how to do this in FREQUENCIES.  I am using FREQUENCIES, because this
*procedure includes calculation of the median among the options
*(DESCRIPTIVE does not).  I would like to analyze about 100 variables
*(mean, SD, skewness, kurtosis, minimum, maximum, median), with the
*resultant table arranged in descending order of the means.


* ANSWER by rlevesque@videotron.ca.

* This syntax sort variables in order of median, then call FREQ with the variables in that order.
* Because of the limit on the number of variables, this syntax only works when there are less than 32,768 cases (that's 2^15 cases).

SET MPRINT=yes /PRINTBACK=yes.
* This is the macro which does the job.
*////////////////////////////////////.
DEFINE !median(!POS=!CMDEND) 

COMPUTE dummy=1.
MATCH FILES FILE=* /BY dummy /KEEP=!1.
XSAVE OUTFILE='c:\\temp\\temp.sav'.

* Count number of variables, assign number to a macro.
FLIP.
COMPUTE dummy=1.
RANK VARIABLES=dummy /N INTO n.
DO IF $CASENUM=1.
WRITE OUTFILE='c:\\temp\\define nbvar.sps'
/ 'DEFINE !nbvar()' n '!ENDDEFINE'.
END IF.
EXECUTE.

GET FILE='c:\\temp\\temp.sav'.
INCLUDE FILE='c:\\temp\\define nbvar.sps'.
* Purpose of next 3 commands is to add a case were all variables have missing values.
COMPUTE dummy=1.
AGGREGATE OUTFILE='c:\\temp\\aggr.sav'
    /BREAK=dummy /n=N(dummy). 
ADD FILES FILE='c:\\temp\\aggr.sav' /IN=aggfile /FILE=* /BY dummy. 

*Replace missing values by median, drop unneeded variables and cases.
RMV m1 TO !CONCAT('m',!EVAL(!nbvar))=MEDIAN(!1, ALL). 
SELECT IF aggfile=1.
MATCH FILES FILE=* /BY=aggfile /DROP=dummy n aggfile.

*Assign median values to their related variables . 
VECTOR v=ALL /m=m1 TO !CONCAT('m',!nbvar).
LOOP #cnt=1 TO !nbvar.
COMPUTE v(#cnt)=m(#cnt).
END LOOP.

* Sort median (D), write a macro in a syntax file which gives the list of variables in that order.
FLIP VARIABLES=!1 .
SORT CASES BY var001 (D).
COMPUTE dummy=1.
MATCH FILES FILE=* /BY dummy /FIRST=first /LAST=last.
DO IF first.
WRITE OUTFILE='c:\\temp\\list.sps' / 'DEFINE !list()' case_lbl.
ELSE if not last.
WRITE OUTFILE='c:\\temp\\list.sps' / " "case_lbl.
ELSE.
WRITE OUTFILE='c:\\temp\\list.sps' /" "case_lbl '!ENDDEFINE'.
END IF.
EXECUTE.

* The rest is a piece of cake.
GET FILE='c:\\temp\\temp.sav'.
INCLUDE FILE='c:\\temp\\list.sps'.
FREQUENCIES
  VARIABLES=!list   /STATISTICS=MEAN MEDIAN SKEWNESS SESKEW KURTOSIS SEKURT
  /ORDER=  ANALYSIS .

!ENDDEFINE.
*////////////////////////////////////.


* This is an example of how to use the macro.

GET FILE='c:\\program files\\SPSS\\employee data.sav'.
!median educ salary TO prevexp.