Bookmark and Share

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


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

Re: st: sortpreserve option


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: sortpreserve option
Date   Thu, 15 Aug 2013 14:37:02 -0400

It's not even that hard; if you are concerned about doing your own sort preserve, just get the sort list at the top of the program

 local sortlist : sort

and then apply at the end

 sort `sortlist'

cheers,
Jeph


On 8/15/2013 12:33 PM, Phil Schumm wrote:
On Aug 14, 2013, at 2:14 PM, Sergiy Radyakin <[email protected]> wrote:
What is the effect of inclusion of the -sortpreserve- option in the declaration of a program, which might change the number of observations? (add or remove)

On Aug 14, 2013, at 5:29 PM, Sergiy Radyakin <[email protected]> wrote:
On Wed, Aug 14, 2013 at 5:15 PM, David Kantor <[email protected]> wrote:
That may or may not be desirable, depending on your needs.

If there is reasonable/predictable/logical behavior of sortpreserve in this case I definitely like to know it.  Whether it is desirable, depends on what it is.


A little introspection suggests that -sortpreserve- works by adding a (temporary) variable to the existing dataset equal to _n, and then simply sorting by this variable when the program returns.  For example, if you manually modify this temporary variable during your program, it has exactly the expected effect (based on what I just described).

Thus, dropping observations during the program leaves the data in their original order (minus those observations), and adding observations results in missing values for the (temporary) sort key, so these appear at the end of the dataset when the program concludes (and, I'd presume, in no predictable order, since -sort- (by default) is not stable).

It would be easy enough to identify the sort key within your program (i.e., as a newly-generated temporary variable) and then modify it for the observations you add, however this strategy would then depend on undocumented behavior of -sortpreserve-, and would therefore inadvisable.  Thus, if you're going to change the data, why not simply implement your own mechanism for ensuring that the data are in the desired order (relative to their initial order) when the program returns?


-- Phil


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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