Bookmark and Share

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

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

Re: st: median of consecutive groups - avoiding loops

From   daniel klein <>
Subject   Re: st: median of consecutive groups - avoiding loops
Date   Thu, 12 May 2011 01:03:50 +0200

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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index