Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: median of consecutive groups - avoiding loops

From   Nick Cox <>
Subject   Re: st: median of consecutive groups - avoiding loops
Date   Thu, 12 May 2011 00:12:10 +0100

Looks good to me. Interesting question remains: whether a loop is faster.


On Thu, May 12, 2011 at 12:03 AM, daniel klein
<> wrote:
> Nick, you are (of course) right. I did not read the messages very
> thoroughly. However, I think I can still offer a solution without
> looping. Might not be the nicest way to do it, but here is my fix
> g int newgroup1 = cond(mod(group, 2), group, group-1)
> g int newgroup2 = cond(mod(group, 2), group-1, group)
> bys newgroup1 : egen med1 = median(value)
> bys newgroup2 : egen med2 = median(value)
> g median = cond(mod(group, 2), med1, med2)
> drop newgroup1 newgroup2 med1 med2
> The median must be corrected for the last group if the number of goups is even.
*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index