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

st: Re: Rowsort question


From   "Michael Blasnik" <michael.blasnik@verizon.net>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: Rowsort question
Date   Sat, 26 Apr 2003 12:49:36 -0400

I think that means just sorting within id by the quantity of interest.  In
the example data, assuming there is an id variable, the reshape approach
could be:

reshape long a , i(id) j(obs)
drop if a==.
bysort id (a): drop if _n<(_N-2)
by id : replace obs=_n
rename a b
reshape wide b, i(id) j(obs)
sort id
save top3

* this saves a file with id b1 b2 b3 with b3 the largest of a1-a6, b2 the
middle, and b1 the smallest, now we put it back into the original file

use originaldata
sort id
merge id using top3

Michael Blasnik
michael.blasnik@verizon.net

----- Original Message -----
From: "Cruces,GA (pgr)" <G.A.Cruces@lse.ac.uk>
To: <statalist@hsphsun2.harvard.edu>
Sent: Saturday, April 26, 2003 11:48 AM
Subject: st: Rowsort question


> Dear all,
>
> I'm picking up a point from the very interesting discussion "Structure
> for making line by line changes?". Scott Merryman suggested using
> rowsort, a command I didn't know. It looks very interesting, though as
> the help file states "rowsort loops over observations and may be
> relatively slow.", so it is still looping over observations...
>
> One point I didn't understand from the help file was:
> "It may be faster to reshape, sort within blocks, and reshape again."
>
> What is exactly "sorting within blocks"?
>
> thank you very much
>
> best,
>
> g.
>
> ****************************************
> From Scott:
>
> No doubt there are more clever ways to do this, but how about:
>
> mvencode _all, mv(-99999) /* or some number smaller than minimum of data
> set
> */
> rowsort a* ,gen(b1 b2 b3 b4 b5 b6) desc
> drop b4-b6
> mvdecode _all, mv(-99999)
>
> You can locate -rowsort- with -findit rowsort-
>
> Scott


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