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, statalist.org 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 <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
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
<klein.daniel.81@googlemail.com> 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index