Statalist


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

RE: st: sorting rows with a "key"


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: sorting rows with a "key"
Date   Wed, 10 Sep 2008 18:04:44 +0100

I guess I should agree with myself in so far as I was correct. (Thanks
to Sergiy for the ipsissima verba; note also that both the programs
cited are on SSC.) 

But in that thread, I believe, or in several discussions of this issue
elsewhere, there was the clear implication that if you want to do what
Ashim wants you might well be better off with a long data structure,
i.e. -reshape-, then -sort-. 

For the specific example that Ashim cites there is a direct solution
without needing any extra program from anywhere. 

gen b1 = cond(key1 <= key2, a1, a2) 
gen b2 = cond(key1 <= key2, a2, a1) 

but I do agree that you need another solution with a hundred. That
solution is to -reshape-, otherwise not just sorting but all kinds of
other analyses will be hopelessly awkward. 

Nick 
[email protected] 

Sergiy Radyakin

see the earlier reply of Nick Cox in the thread:
http://www.stata.com/statalist/archive/2008-08/msg01169.html

In particular this paragraph:
------- quote NJC --------------
3. -rowsort- and -sortrows- are not intended for this problem in which
Sergiy has

motherid   child_id1 child_id2 child_id3 age1 age2 age3 gender1 gender2
gender3
.....
and might want to sort children by age keeping their ordering stable
and moving their (string) ids and (numeric) gender dummies in sync.

They are indeed useless at that problem. But see -rowranks- from SSC.
That's not intended, quite, for that problem either, but it is perfectly
soluble using basic Stata.
------- end quote NJC --------------


On Wed, Sep 10, 2008 at 2:01 AM, Ashim Kapoor <[email protected]>
wrote:
>
> I really liked the program "sortrows" . I
> wish to ask if anyone has written a similar program for sorting with a
> "key".
>
> I mean the following.:=
>
> say I have 4 variables ( in my case I have around a 100 so I cannot
> possibly do it in the usual manual bubble sort because it takes
> forever)
>
> say a1 a2 key1 key2
>
> I want to say sortrows ( key1 key2)  which will do the usual sortrows
> BUT it will also sort the a1 and a2 in the same order ie. if k1 < k2
> then a1 < a2 for that row.
>
> Any tips ? Any program which already does this ?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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