------------------------------------------------------------------------------- help forfixsort-------------------------------------------------------------------------------

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

fixsortvarlist[ifexp] [inrange],generate(newvarlist)[missingreverse]

Description

fixsortsorts each variable invarlistand 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

varlistretain their current sort order. The sort order of the dataset is unaffected. Variables may be numeric or string.

Remarks

fixsortis 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.

fixsortis 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

fixsortmay readily possess different numbers of non-missing values. See also themissingoption below.

ifandinact only to select observations and do not affect the storage of new variables in observations 1 up.By conscious design, there is no

replaceoption to let users overwrite existing variables. Thus no misunderstanding offixsortshould mean that users lose data.

Options

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

missingspecifies 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 byifand/orin) are created as "" or . (i.e. system missing).

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

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

a b c3 7 13 1 8 12 2 9 11After

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

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

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

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

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

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

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