Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: median of consecutive groups - avoiding loops


From   Nick Cox <[email protected]>
To   [email protected]
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.

Nick

On Thu, May 12, 2011 at 12:03 AM, daniel klein
<[email protected]> 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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index