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 11:29:34 -0400

Perhaps this is naive of me, but the documentation for -sortpreserve- states up front that -sortpreserve- is not intended for programs which modify the data, nor is it appropriate in that case: "sortpreserve is intended for use in programs that report results or add new variables to the dataset, not programs that modify the data in memory."

hth,
Jeph


On 8/15/2013 10:48 AM, David Kantor wrote:
Disregard the prior message from me. It got sent prematurely.

At 06:29 PM 8/14/2013, you wrote:
On Wed, Aug 14, 2013 at 5:15 PM, David Kantor <[email protected]> wrote:
> At 03:14 PM 8/14/2013, Sergiy Radyakin 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)
>
>
> My understanding is that it should have no effect on the number of
> observations.
Dear David, just to make sure we are talking about the same thing. The
program _is expected_ to make changes to data by adding/removing
observations. Can I still declare it -sortpreserve-?
Specifically, what will happen to the newly added observations? will
they end up in the end of the restored-order-dataset? in the beginning
of it? (elsewhere if other possibilities exist?)

Interesting question. I don't know the answer; I never considered that, but I can imagine some possibilities. It may pay
attention to the sorted-by attribute, or it may pay attention to the actual order. In the former case, you may ask, what
happens if one of the sort variables is dropped -- or altered? For the latter possibility, other questions arise; where
do the new observations go? And does it matter how they were created?
This is where, if I needed to know the answers, I would run some experiments.

I think you can always declare a program as sortpreserve. I don't think it will crash. But again, I'd experiment.


> Its effect is to insure that when the program exits, the data are left in
> the same sort order as before.

Yes, that's by the book. But we now have more data, so how would Stata
know where to put the new observations which it hasn't seen before?

Yes, I gave you the book answer, which you already knew. My own inclination is that sortpreserve is appropriate only
when the set of observations is not altered by the program. But that is evading your question.
I'll be curious to hear about what you find.

> 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.

I just meant that, based on my own programs (funcdep, in particular) and how they are used, there are times when either...
a, you don't want Stata to do the extra work of putting the data back into its original order, especially if the set is
large, and if you are just going to do another sorting afterward, or you just don't need it in any particular order.
b, you actually want the dataset to be left in the order that the program imposed; it would be counterproductive to undo
that sorting.


Best, Sergiy Radyakin

> It may take a little extra run-time.
>
> I have written one program where I made it an option -- whether to run it as
> sortpreserve. There was a little trick to make that happen, which I can
> explain if you want.

I envision this as:
http://radyakin.org/statalist/2013081401/myprog.ado
with any sort-destructive actions to be taken in the common part.
Perhaps it can be more elegant, but this is not what I am looking for now.

Thank you for your help
Sergiy

Indeed, your example is like what I had in mind. I believe that it can be abbreviated a bit, though.

Best,
--David

*
*   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