Statalist


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

Re: st: RE: A query about sorting.


From   "Eva Poen" <[email protected]>
To   [email protected]
Subject   Re: st: RE: A query about sorting.
Date   Mon, 25 Aug 2008 14:53:01 +0100

You can modify Maarten's code to achieve that. I called the new
variables vnew1, vnew2 and vnew3 below:


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


// do the sort
list
gen i = _n
reshape long v a, i(i) j(var)

// sort according to a:
bysort i (a): gen sort = _n

// gen vnew:
gen vnew = .
forvalues x = 1/3 {
  bysort i (var) : replace vnew = v[`x']  if sort == `x'
}

drop sort
reshape wide v vnew a , i(i) j(var)

drop i
order v1 v2 v3 vnew1 vnew2 vnew3 a1 a2 a3
list
**************

Hope this helps,
Eva



2008/8/25 Ashim Kapoor <[email protected]>:
> I have another question. I have 3 variables v1 v2 v3 and 3 keys a1 a2
> a3. So now I want for each row, v1 v2 v3 sorted according to a1 a2 a3.
>
> ie. If for some row I have  a2 < a1 is < a3 then I want v1 to have the
> value v2 , v2 to have the value of v1 and v3 as it is in that row.
>
> Would you know how to do this ?
>
> Thank you,
> Ashim.
>
>
>
> On Mon, Aug 25, 2008 at 5:50 PM, Martin Weiss
> <[email protected]> wrote:
>> -ssc d rowsort-
>>
>> BTW, what is a "simple bubble"?
>>
>> -----Original Message-----
>> From: [email protected]
>> [mailto:[email protected]] On Behalf Of Ashim Kapoor
>> Sent: Monday, August 25, 2008 2:07 PM
>> To: [email protected]
>> Subject: st: A query about sorting.
>>
>> Hello everyone,
>>
>> 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. Any
>> comments anyone ?
>>
>> Thank you guys,
>> Ashim. : )
*
*   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