Statalist


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

Re: st: A query about sorting.


From   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: A query about sorting.
Date   Mon, 25 Aug 2008 13:28:50 +0100 (BST)

--- Ashim Kapoor <ashimkapoor@gmail.com> wrote:
> I have a simple code to write.
> 
> I have 6 variables v1, v2 , v3, v4 ,v5 , v6.
> 
> I want to sort "according to each observation". That is , in the end
> for each i I want , v1[i] < = v2[i] <=... v6[i].
> 
> So I wrote a simple bubble for each row and then loop from 1 to _N.
> 
> Now the problem is that this works but it's VERY VERY slow. 

This could be done using a combinatin of -reshape- and -by-, like in
the example below. This should be faster.

*--------------- begin example ---------------
// create some random data
drop _all
set obs 10
forvalues i = 1/3 {
	gen v`i' = floor(10*uniform())
}

// do the sort
list
gen i = _n
reshape long v, i(i) j(var)
drop var
gen v2 = -v /*to sort from highest to lowest*/
bys i (v2): gen var = _n
drop v2
reshape wide v, i(i) j(var)
drop i
list
*----------------- end example ----------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------

Send instant messages to your online friends http://uk.messenger.yahoo.com 
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index