Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: error with -egen median if-


From   "Alex Ogan" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: error with -egen median if-
Date   Mon, 22 May 2006 13:14:34 -0400

Thanks for confirming this.  I tried it in Stata 9 as well, and had the
same problem.  

Your _gmed6 program below seems to work fine -- at least it has no
problem with the simple test case with the -if- clause using a string
variable.  In the production program I'm writing, I elected to create a
numeric tempvar and set it based on the -if- clause, then use the
tempvar with the egen command.

Weird bug.  

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Austin
Nichols
Sent: Monday, May 22, 2006 12:58 PM
To: [email protected]
Subject: Re: st: RE: error with -egen median if-

Alex Ogan:
The bug is reproducible, it seems. Note that the median function for
-egen- is implemented as _gmedian.ado and is written in Stata 3 code,
which is the version I started in, but no longer remember... let's
rewrite it in Stata 6 code and see if that works.  Save the following
as _gmed6.ado in your c:\ado folder or equivalent, and try your code
again using egen y=med6(x).

program define _gmed6
 version 6, missing
 syntax newvarname =/exp [if] [in] [, BY(varlist)]
 tempvar touse x n
 quietly {
  gen byte `touse'=1 `if' `in'
  gen double `x' = `exp'
  sort `touse' `by' `x'
  by `touse' `by': gen long `n'=sum(`x'<.)
  by `touse' `by': g `varlist' = ( /*
   */ `x'[(`n'[_N]+1)/2] + /*
   */ `x'[(`n'[_N]+2)/2] ) / 2 if `touse'==1
 }
end

On 5/22/06, Alex Ogan <[email protected]> wrote:
> Here is a more simple example.  I also should have mentioned I am
using
> Stata/SE 8.2.
> . egen testsum_1 = sum(numvar)
> . egen testmedian_1 = sum(numvar)
> . egen testsum_2 = sum(numvar) if strvar=="a"
> (2 missing values generated)
> . egen testmedian_2 = median(numvar) if strvar=="a"
> a not found
> r(111);

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


This message is intended solely for the designated recipient(s). It may contain confidential or proprietary information and may be subject to confidentiality protections. If you are not a designated recipient, you may not review, copy, or distribute this message. If you receive this in error, please notify the sender by reply e-mail and delete this message. 

Arrowstreet Capital, L.P. is an Equal Opportunity Employer. 



*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index