Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: random order after sub-sorting


From   Phil Schumm <pschumm@uchicago.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: random order after sub-sorting
Date   Fri, 12 Aug 2005 12:20:43 -0500

At 6:03 PM +0200 8/12/05, D.C.J. Vissers wrote:
1. Why does Stata randomly order records after a certain sorting?

After issuing the following command:

sort varlist

any observations with equal values of varlist will be in random order. That's just how -sort- works. There is an FAQ on this issue (http://www.stata.com/support/faqs/lang/sort.html), and it is a common gotcha even among experienced programmers. If instead you want Stata to leave observations with tied values of varlist in the same order as before, use the -stable- option (introduced in version 8, I believe).



2. How can I make sure that every time the same selection is made?

As the aforementioned FAQ describes, whenever you are selecting cases based on their order, you should insure that that order is unambiguous (read reproducible). In some cases, this can be accomplished by adding additional variables to your -sort- command; often, the choice of variable(s) is straightforward (e.g., time, or perhaps in your case, status_of_wife). And in certain cases, it can be helpful to create a variable (perhaps temporary) like this:


gen record_no = _n


immediately after you read in your data (e.g., using -use-, -insheet-, or -infix-). This can then be used later on, either by itself or in conjunction with other variables, whenever it is necessary to establish a reproducible order among the observations.


-- Phil
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index