Thanks to Kit Baum, SSC now offers an alternative command to sort and gsort.
TITLE
'USORT': module to perform locale-based ascending and descending sort that
supports conditional statements, observation ranges, and user-defined
handling of substrings and missing values
DESCRIPTION/AUTHOR(S)
This program is a byable sort command, which allows a) custom
first and last substrings, including system (.) and all remaining
missing values, b) gsort-like syntax for the ascending and
descending order, as well as c) conditional [if] and range [in]
sorting. The program is built around the Stata sort command and
adds the data-sorted flag (sorted by) to the dataset if all rows
are selected and applies Mata _collate() otherwise. Sorting large
datasets might be taxing on machine memory or disk space.
KW: sort
KW: conditional
KW: descending order
EXAMPLES
TITLE
'USORT': module to perform locale-based ascending and descending sort that
supports conditional statements, observation ranges, and user-defined
handling of substrings and missing values
DESCRIPTION/AUTHOR(S)
This program is a byable sort command, which allows a) custom
first and last substrings, including system (.) and all remaining
missing values, b) gsort-like syntax for the ascending and
descending order, as well as c) conditional [if] and range [in]
sorting. The program is built around the Stata sort command and
adds the data-sorted flag (sorted by) to the dataset if all rows
are selected and applies Mata _collate() otherwise. Sorting large
datasets might be taxing on machine memory or disk space.
KW: sort
KW: conditional
KW: descending order
EXAMPLES
Code:
Setup: . sysuse auto Sort observations in ascending order by price: . usort price Sort observations in ascending order by make in Czech, grouped by foreign, with VW models placed at the top: . bysort foreign: usort make, first(VW, pos) loc(cs_CS) Sort observations in descending order by mpg and price: . usort -mpg -price Sort observations in descending order by price for domestic cars only: . usort -mpg -price if ! foreign
Comment