SORT CASES BY var_list[({D|A}] [ var_list[({D|A}] ] ...
SORT CASES
sorts the active dataset by the values of one or more
variables.
Specify BY
and a list of variables to sort by. By default, variables
are sorted in ascending order. To override sort order, specify (D)
or
(DOWN)
after a list of variables to get descending order, or (A)
or (UP)
for ascending order. These apply to all the listed variables
up until the preceding (A)
, (D)
, (UP)
or (DOWN)
.
The sort algorithms used by SORT CASES
are stable. This means
records which have equal values of the sort variables have the
same relative order before and after sorting. Thus,
re-sorting an already sorted file does not affect the ordering of
cases.
SORT CASES
is a procedure. It causes the data to be read.
SORT CASES
attempts to sort the entire active dataset in main memory.
If workspace is exhausted, it falls back to a merge sort algorithm which
creates numerous temporary files.
SORT CASES
may not be specified following TEMPORARY
.
In Example 12.6 the data from the file physiology.sav is sorted by two variables, viz sex in descending order and temperature in ascending order.
get file='physiology.sav'. sort cases by sex (D) temperature(A). list. |
In Results 12.4 you can see that all the cases with a sex of ‘1’ (female) appear before those with a sex of ‘0’ (male). This is because they have been sorted in descending order. Within each sex, the data is sorted on the temperature variable, this time in ascending order.
|
Note that SORT CASES
, like all other transformations, affects only the active file.
It does not have any effect upon the physiology.sav file itself. For that, you
would have to rewrite the file using the SAVE
command (see SAVE).
When using the graphic user interface, it is often simpler to perform a sort directly from the data view. To do this, switch to the data view. Select the column corresponding to the variable by which you want to sort and click button 1 and then click button 3. A popup menu will appear like that shown in Screenshot 12.5. Select either “Sort Ascending” or “Sort Descending” from this menu.
However, sometimes you will want to sort on two or more variables, and that is not possible using this method. In this case, you must either use some code or the “Sort Cases” dialog from the Data menu. Screenshot 12.6 shows the dialog box set up to perform a sort on both sex and height. Note that the order in which you enter the variables is important. In this case, the data will be first sorted on sex, and then all cases for which sex is the same will then be sorted by height.