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

Re: st: A query about sorting.

From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: A query about sorting.
Date   Mon, 25 Aug 2008 13:28:50 +0100 (BST)

--- Ashim Kapoor <[email protected]> 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
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
*----------------- end example ----------------
(For more on how to use examples I sent to the Statalist, see )

Hope this helps,

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

Send instant messages to your online friends 
*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index