Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: by prefix

From   Richard Williams <>
To, <>
Subject   Re: st: by prefix
Date   Mon, 04 Jun 2012 10:46:12 -0500

At 09:35 AM 6/4/2012, tashi lama wrote:


Here is sth I found Stata very unintelligent. A by prefix has to be preceded by sort command although the dataset is already sorted by design. Take a look at this

var1    var2

A         3

A         4

A         1

B         2

B         5

The dataset is already sorted by var1 as you all can see. Now when I apply by prefix

by var1: summ var2    says not sorted.

Stata obliges you to write

bysort var1: summ var2    which I think is overkill. Any thoughts?

Yes, but Stata doesn't know it is already sorted. The data manual entry for -sort- says this:

"Stata does not track changes in the sort order and will sometimes decide that a dataset is not sorted when, in fact, it is. For instance, if we were to change the first observation of our auto dataset from 12 miles per gallon to 10, Stata would decide that the dataset is "Sorted by:" nothing, just as it did above when we changed mpg from 12 to 13. Our change in example 2 did change the order of the data, so Stata was correct. Changing mpg from 12 to 10, however, does not really affect the sort order.

"As far as Stata is concerned, any change to the variables on which the data are sorted means that the data are no longer sorted, even if the change actually leaves the order unchanged. Stata may be dumb, but it is also fast. It sorts already-sorted datasets instantly, so Stata's ignorance costs us little."

Richard Williams, Notre Dame Dept of Sociology
OFFICE: (574)631-6668, (574)631-6463
HOME:   (574)289-5227
EMAIL:  Richard.A.Williams.5@ND.Edu

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index