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   Wed, 11 May 2011 19:01:16 +0100

I don't see what's wrong with writing a loop. I'm going to assume
groups go 1, 2, up with no gaps. If that's not true, use -egen,
group()- to generate such a variable.

su group, meanonly
local last = r(max) - 1

gen mymedian = .

qui forval i = 1/`last' {
        local j = `i' + 1
        su value if inlist(group, `i', `j') , detail
        replace mymedian = r(p50) if group == `i'
}

A non-loop solution would be nice too, but because it's medians, it
seems unlikely. Show that I am wrong....

What to do with the last group is left undefined here. Using
-summarize- is not obligatory. Mata might be faster.

Nick


On Wed, May 11, 2011 at 6:47 PM, Sarah Kristina Reuter
<[email protected]> wrote:

> I do not know if the following is possible, I hope anyone has an idea how to
> address it.
> I would like to create a variable that gives me the median of two
> consecutive groups for every group. Here is an example:
> id         group   value   median
> 1          1          5          ?
> 2          1          7          ?
> 3          1          8          ?
> 4          2          3          ??
> 5          2          5          ??
> 6          3          5          ???
> 7          3          7          ???
> 8          3          4          ???
> 9          3          2          ???
> 10        3          8          ???
>
>
> The first ? should be the median of id 1-5, the ?? the median of id 4-10 and
> the ??? the one of id 6-10.
> Does anyone know a command that would do this without needing to write a
> loop?

*
*   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