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.

# Re: st: median without current observation

 From Andres Silva To statalist@hsphsun2.harvard.edu Subject Re: st: median without current observation Date Mon, 15 Apr 2013 11:23:14 +0200

```Hi Nick and Federico,
Thank you!!!, Andres

On Mon, Apr 15, 2013 at 11:17 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> Federico and I made different guesses about the data structure and
> what is needed.
>
> In Federico's case, the code can be simplified, I think
>
> levelsof region, local(reglist)
> gen me_y = .
> foreach r of local reglist {
>        su y if region != `r', detail
>        replace me_y  = r(p50) if region==`r'
> }
>
> Nick
> njcoxstata@gmail.com
>
> On 15 April 2013 09:22, Federico Belotti <f.belotti@gmail.com> wrote:
>
>> The following code creates R mediany variables (R is the number of regions), where each variable takes the median of the distribution of "y" excluding the value of region `r'.
>>
>> levelsof region, local(reglist)
>> foreach r of local reglist {
>>
>>         egen me_y`r' = median(y) if region!=`r'
>>         tempvar tmp
>>         egen "`tmp'" = max(me_y `r')
>>         replace me_y `r' = `tmp' if region==`r'
>>
>> }
>>
>> hope this helps.
>> Federico
>>
>> On Apr 15, 2013, at 9:29 AM, Andres Silva wrote:
>>
>>> Dear statausers,
>>>
>>> I want to create a variable "mediany", which needs to be regional
>>> median (without considering the current value of "y"). I mean I need
>>> to calculate the median with the remaining values of "y" in each
>>> specific region.
>>>
>>> by region: egen mediany=median(y)
>>> How can I tell stata not use the current "y" value?
>>>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```