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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: RE: AW: RE: Re: Generate Variable |

Date |
Fri, 13 Mar 2009 14:23:44 -0000 |

In fact, the -egen- call should go altogether. For example, gen numProp4 = numProp if wavenum == 4, by(id) bysort id (numProp4) : replace numProp4 = numProp4[1] is as much as is to be needed. -egen- was invoked because egen numProp4 = total(numProp * (wavenum == 4)), by(id) gets you there in one line whenever there is precisely one non-missing value for wavenum 4 in each panel -- at the expense of lots of extra lines off-stage. But once you are realistic about missings, retreating to a first principles approach becomes a much better strategy. Nick n.j.cox@durham.ac.uk Nick Cox [Martin] raises a good question about missings. Here's a better approach if missings are present: egen numProp4 = mean(numProp) if wavenum == 4, by(id) bysort id (numProp4) : replace numProp4 = numProp4[1] egen numInsArrng6 = mean(numInsArrng) if wavenum == 6, by(id) bysort id (numInsArrng6) : replace numInsArrng6 = numInsArrng6[1] gen myindicator = numProp4 < numInsArrng6 if !missing(numProp4, numInsArrng6) Martin Weiss Nick Cox Another approach: I infer panel structure with an identifier, and at most one measurement for each -wavenum-. If that is so egen numProp4 = total((wavenum == 4) * numProp) , by(id) and egen numInsArrng6 = total((wavenum == 6) * numInsArrng) , by(id) spread values in particular wave numbers to all observations with the same identifier. For example, wavenum == 4 is 1 if wavenum is 4 and 0 otherwise, so the ensuing total is just the value of numProp for wavenum == 4. The required indicator variable follows directly. Note that even Erick's -if- condition had been correct (it isn't), his expression for indicator (dummy in his terminology) would have yielded 1 and missing, not 1 and 0. * * 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/

**References**:**st: Generate Variable***From:*<erickg@uchicago.edu>

**st: Re: Generate Variable***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**st: RE: Re: Generate Variable***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**st: AW: RE: Re: Generate Variable***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**st: RE: AW: RE: Re: Generate Variable***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**st: RE: Stata output into Word** - Next by Date:
**st: RE: Stata output into Word** - Previous by thread:
**Re: st: AW: RE: AW: RE: Re: Generate Variable** - Next by thread:
**st: save coeff estimators of Arima model with foreach** - Index(es):

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