help for fixsort

Sort variables and align in sorted order, with others fixed in position

fixsort varlist [if exp] [in range] , generate(newvarlist) [ missing reverse ]


fixsort sorts each variable in varlist and generates new variables so that all new variables are in sorted order, by default aligned so that the first value of each is in the first observation, the second value of each is in the second observation, and so on. Reverse sorting (last value first, etc.) is also possible.

Any variables not in varlist retain their current sort order. The sort order of the dataset is unaffected. Variables may be numeric or string.


fixsort is a rarely needed command. Almost all sorting problems call for sort or gsort. An example where it may be appropriate is in sorting independent batches of random numbers held in separate variables.

fixsort is unusual in various ways:

Whatever is stored in any observation for new variables has no necessary relationship to values for other variables in the same observation.

By default missing values of any kind (. .a ... .z "") are all ignored. Any such missing values are represented in new variables by values of . or "", as appropriate to the variable type, all stored in the later observations of the dataset. Note that treatment of missing values is variable by variable, so that the new variables created by fixsort may readily possess different numbers of non-missing values. See also the missing option below.

if and in act only to select observations and do not affect the storage of new variables in observations 1 up.

By conscious design, there is no replace option to let users overwrite existing variables. Thus no misunderstanding of fixsort should mean that users lose data.


generate() specifies the names of new variables to hold the sorted values of varlist. generate() is a required option.

missing specifies that missing values of any kind are to be included in the sorting. Missing values are treated as is; any other missing values (corresponding to observations excluded by if and/or in) are created as "" or . (i.e. system missing).

reverse specifies sorting in reverse order, i.e. highest values first.


Suppose we start in each of the following with these data:

a b c 3 7 13 1 8 12 2 9 11

After fixsort a b c, gen(A B C) we would have

a b c A B C 3 7 13 1 7 11 1 8 12 2 8 12 2 9 11 3 9 13

After fixsort c in 2/3, gen(C23) we would have

a b c C23 3 7 13 11 1 8 12 12 2 9 11 .


Philippe Van Kerm provoked a reconsideration and rewriting of this program.


Nicholas J. Cox, Durham University, U.K. n.j.cox@durham.ac.uk

Also see

On-line: help for sort, gsort, clsort (if installed)