Skip to main content

Research Repository

Advanced Search

Speaking Stata: Ordering or ranking groups of observations

Cox, Nicholas J.

Speaking Stata: Ordering or ranking groups of observations Thumbnail



Results for categorical variables may often be clearer if those variables are reordered or reranked, say, according to some measure of absolute or relative frequency or according to summaries of some other variable. Some graphical and tabulation commands have dedicated options serving that end. Otherwise, in practice a new order is often best achieved by creating a new variable holding the desired order using one or another egen function. There is usually a need to preserve the information in existing values or value labels and to watch out for ties. There may be a desire to reverse the direction of ranking from the default. I discuss procedures for datasets based on aggregate frequencies and for datasets based on individuals and introduce a new convenience command, myaxis, that handles many cases directly.


Cox, N. J. (2021). Speaking Stata: Ordering or ranking groups of observations. The Stata Journal, 21(3), 818-837.

Journal Article Type Article
Acceptance Date Aug 25, 2021
Online Publication Date Oct 4, 2021
Publication Date Sep 1, 2021
Deposit Date Jan 19, 2022
Publicly Available Date Jan 20, 2022
Journal The Stata Journal: Promoting communications on statistics and Stata
Print ISSN 1536-867X
Electronic ISSN 1536-8734
Publisher SAGE Publications
Peer Reviewed Peer Reviewed
Volume 21
Issue 3
Pages 818-837
Public URL


Published Journal Article (Advance online version) (392 Kb)

Publisher Licence URL

Copyright Statement
Advance online version This article is distributed under the terms of the Creative Commons Attribution 4.0 License ( which permits any use, reproduction and distribution of the work without further permission provided the original work is attributed as specified on the SAGE and Open Access pages.

You might also like

Downloadable Citations