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

From |
"Martin Weiss" <Martin.Weiss1@gmx.de> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: RE: st: bys and [_N] within replace |

Date |
Thu, 19 Mar 2009 10:25:34 +0100 |

<> I think it is the appropriate way to end the thread. If you answered them all individually, it would only increase the traffic on the list w/o adding to the insights gained from this thread. HTH Martin -------- Original-Nachricht -------- > Datum: Thu, 19 Mar 2009 08:05:44 +0100 > Von: Alexander.Severinsen@telenor.com > An: statalist@hsphsun2.harvard.edu > Betreff: RE: st: bys and [_N] within replace > Johannes, > > Yes, now both yours and Eva's code produces the same result. Thank you for > the help solving my problem. You do mention that Evas code do not need to > be sorted by the time variable, and in this particular example it does not > matter. Though, if I understand Nick Cox correct, it does not hurt either, > and may occasionally prevent from being biten. > > Also, thanks to Johannes, Eva and Nick for giving additional advice about > how interpret _N and _n to look within observations in a bysort, and for > further coding advice. Very helpful. Stata (and Statalist!) never fails to > impress. > > As a sidenote. Including Johannes posting on this matter I received three > answers to this posting. Is this the approriate way to end the thread, or > is it more appropriate to answer all individual postings? > > Thanks for your consideration. > > Best wishes, > Alexander Severinsen > > -----Opprinnelig melding----- > Fra: owner-statalist@hsphsun2.harvard.edu > [mailto:owner-statalist@hsphsun2.harvard.edu] På vegne av Johannes Geyer > Sendt: 18. mars 2009 14:30 > Til: statalist@hsphsun2.harvard.edu > Emne: RE: st: bys and [_N] within replace > > > I tried your code as well, but it does not replicate the above > > solution, most likely because my statement of the problem was > > unclear. Sorry about that. > > No, I think that I made an error. I implicitly assumed in my code that > each individual would have an occurrence of "TM Ei-svar" - which might not > be correct. > > The following code should produce the same results as Eva's code: > > ******************************* > bys Id Result2: gen helpvar = _n == 1 > egen kun_eisvar = total(helpvar), by(Id) > replace kun_eisvar = kun_eisvar == 1 & Result2 == "TM Ei-svar" > drop helpvar > ******************************* > > Note that it is not necessary to sort by the time variable. Eva's code > could be written as: > > ******************************* > gen other = Result2 != "TM Ei-svar" > bysort Id : replace other = sum(other) > by Id : gen dummy = other[_N]==0 > ******************************* > > > I am still puzzled about why my code did not work. I thought that I > > could use _N to look forward and _N-1 to look backwards, within Id > > and Commdate. > > "_N" simply contains the total number of observations in the dataset or in > your "by id - group". So -[_N-1]- used as an index means that you refer > to the second last observation in the group you are looking at. And -[_N]- > is the last observation. So you don't step through all observations of an > id if you have more than two realizations of it. > > > Johannes > > > owner-statalist@hsphsun2.harvard.edu schrieb am 18/03/2009 14:05:15: > > > Johannes, thanks for this. > > > > Eva Poen gave me this code, > > > > gen other = Result2 != "TM Ei-svar" > > bysort Id (Commdate) : replace other = sum(other) > > by Id (Commdate) : gen dummy = other[_N]==0 > > drop other > > > > which works fine. > > > > > > > I am still puzzled about why my code did not work. I thought that I > > could use _N to look forward and _N-1 to look backwards, within Id > > and Commdate. > > > > Anyway, problem solved. Thanks for the help. > > > > Best wishes, > > Alexander Severinsen > > > > > > -----Opprinnelig melding----- > > Fra: owner-statalist@hsphsun2.harvard.edu [mailto:owner- > > statalist@hsphsun2.harvard.edu] På vegne av Johannes Geyer > > Sendt: 18. mars 2009 11:28 > > Til: statalist@hsphsun2.harvard.edu > > Emne: Re: st: bys and [_N] within replace > > > > you could do the following: > > > > bys Id Result2: gen helpvar = _n == 1 > > egen kun_eisvar = total(helpvar), by(Id) > > replace kun_eisvar = kun_eisvar == 1 > > drop helpvar > > > > > > Hope this helps, > > > > Johannes > > > > > > > > > > > > ---------------------- > > Johannes Geyer > > Deutsches Institut für Wirtschaftsforschung (DIW Berlin) > > German Institute for Economic Research > > Department of Public Economics > > DIW Berlin > > Mohrenstraße 58 > > 10117 Berlin > > Tel: +49-30-89789-258 > > > > owner-statalist@hsphsun2.harvard.edu schrieb am 18/03/2009 11:05:24: > > > > > I have the following panel. > > > > > > > > > Id Commdate Result2 > > > 400 05may2008 TM FAILURE > > > 400 29may2008 DM FAILURE > > > 400 05oct2008 TM Ei-svar > > > 400 27oct2008 TM Ei-svar > > > 400 30nov2008 TM Ei-svar > > > 400 04jan2009 TM Ei-svar > > > > > > I would like to generate a binary variable that equals 1 if a > > > category "TM Ei-svar" within result2 occurs. But I only want this to > > > happen if no other category in Result2 is present within Id, either > > > onwards are backwards in time (Commdate). Nick Cox and Martin Weiss > > > gave excellent advice on an earlier related problem pointing me to > > > 'bys' and using [_N] within replace statements. Nick also refered me > > > to further free 'by' tricks at > > > > > > http://www.stata-journal.com/sjpdf.html?articlenum=pr0004 > > > > > > However, I am still unable to solve the above problem. I tried > > > > > > gen kun_eisvar=0 > > > bys Id (Commdate):replace kun_eisvar=1 if /// > > > (result2=="TM Ei-svar"& /// > > > result2[_N]!="TM FAILURE"&result2[_N-1]!="TM FAILURE"& /// > > > result2[_N]!="TM SUCCESS"&result2[_N-1]!="TM SUCCESS"& /// > > > result2[_N]!="SUCCESS DM SMS 1999"&result2[_N-1]!="SUCCESS DM > > SMS1999"& /// > > > result2[_N]!="DM SUCCESS NY BIND"&result2[_N-1]!="DM SUCCESS NY > > BIND"& /// > > > result2[_N]!="DM SUCCESS"&result2[_N-1]!="DM SUCCESS"& /// > > > result2[_N]!="DM FAILURE"&result2[_N-1]!="DM FAILURE") > > > > > > though, this does not work. See an example below. > > > > > > Id Commdate Result2 kun_eisvar > > > 400 05may2008 TM FAILURE 0 > > > 400 29may2008 DM FAILURE 0 > > > 400 05oct2008 TM Ei-svar 1 > > > 400 27oct2008 TM Ei-svar 1 > > > 400 30nov2008 TM Ei-svar 1 > > > 400 04jan2009 TM Ei-svar 1 > > > > > > Because Result2 has other categories than 'TM Ei-svar' for Id=400 > > > this observation should not equal 1. > > > > > > Any advice on where I went wrong is appreciated. > > > > > > > > > Best wishes, > > > Alexander > > > > > > > > > > > > > > > > > > * > > > * 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/ > > > > > > * > > * 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/ > > > > * > > * 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/ > > > * > * 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/ > > * > * 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/ -- Martin Weiss Hackersteigle 3 72076 Tübingen 00497071/793535 00491784597218 Psssst! Schon vom neuen GMX MultiMessenger gehört? Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger01 * * 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**:**RE: st: bys and [_N] within replace***From:*<Alexander.Severinsen@telenor.com>

**RE: st: bys and [_N] within replace***From:*Johannes Geyer <JGeyer@diw.de>

**RE: st: bys and [_N] within replace***From:*<Alexander.Severinsen@telenor.com>

- Prev by Date:
**Re: st: Bootstrap after xtregar** - Next by Date:
**st: Ado-files for generating spatial effect variables** - Previous by thread:
**RE: st: bys and [_N] within replace** - Next by thread:
**Re: st: bys and [_N] within replace** - Index(es):

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