Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: gsort issue


From   "Brian P. Poi" <[email protected]>
To   [email protected]
Subject   RE: st: gsort issue
Date   Fri, 6 Jul 2007 16:38:04 -0500 (CDT)

On Thu, 5 Jul 2007, Fred Wolfe wrote:

...

I remember (from the old days) that -gsort- didn't set the sort order. But sometime ago it was changed, or was it? gsort help says that it "... differs from sort in that sort produces ascending-order arrangements only."

However, the following code produces ?unexpected results:

. sort age duration
. local sortorder : sort
. di "sort order is `sortorder'"
sort order is age duration

. gsort -age duration
. local sortorder : sort
. di "sort order is `sortorder'"
sort order is

...

So it seems that gsort sets the sort order only in ascending sorts. Either that or the reported output is wrong.

-gsort- will only set the sort order flag when it sorts in ascending order. Internally Stata considers the data to be sorted in ascending order or not sorted at all. Stata commands that require sorted data need the data to be in ascending order. For example, the -by:- prefix requires
that the data be sorted in ascending order of the by variable(s).

If you -gsort- a variable or variables in ascending order, the sort order flag gets set because other Stata commands and features that rely on the data being sorted can then work.

But if you -gsort- in descending order, as far as Stata commands that require the data be sorted are concerned, your data are not sorted because they are not sorted in ascending order. So in this case -gsort- does not set the sort order flag.

-- Brian Poi
[email protected]


*
* 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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index