Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Riccardo Valboni <rvalboni@rsm.nl> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | RE: st: creation of a new variable using a variable condition |
Date | Fri, 21 Feb 2014 21:11:11 +0000 |
Dear Nick, Many thanks for your answer! And what if I would like newvar to be a counting variable that counts the years from the last event(see example)? How would you do that? id year event newvar a 2001 0 0 a 2002 0 0 a 2003 1 1 a 2004 1 1 b 2005 0 0 b 2006 1 1 b 2007 0 2 b 2008 0 3 b 2009 0 4 Thank you. Riccardo -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: venerdì 21 febbraio 2014 16:14 To: statalist@hsphsun2.harvard.edu Subject: Re: st: creation of a new variable using a variable condition One way to approach this is bysort id (year): gen newvar = sum(event) >= 1 See also http://www.stata.com/support/faqs/data-management/first-and-last-occurrences/ Nick njcoxstata@gmail.com On 21 February 2014 15:04, Riccardo Valboni <rvalboni@rsm.nl> wrote: > Dear statalist, > > I am trying to do something that it's probably basic but I cannot get to the bottom of it. > I have a typical unbalanced panel dataset. Some of my observations happen to have an event at a certain point in time (coded 1 in the 'event' column). For them I would like to generate a new variable (newvar) that takes value 1 from the year of the first event onwards. The year of the first event obviously changes from case to case. (see the example below) > > id year event newvar > a 2001 0 0 > a 2002 0 0 > a 2003 1 1 > a 2004 1 1 > b 2005 0 0 > b 2006 1 1 > b 2007 0 1 > b 2008 0 1 > b 2009 0 1 > ... > > I imagine that the command to do this starts with: by id... but I wouldn't know how continue that. Any suggestions? > > Thank you! > Riccardo > > > > > Disclaimer > ________________________________ > De informatie verzonden in dit e-mail bericht is vertrouwelijk en is uitsluitend bestemd voor de geadresseerde van dit bericht. Lees verder: www.eur.nl/email-disclaimer <http://www.eur.nl/email-disclaimer> > The information in this e-mail message is confidential and may be legally privileged. Read more: www.eur.nl/english/email-disclaimer<http://www.eur.nl/english/email-disclaimer> > ________________________________ > > > * > * 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/ Disclaimer ________________________________ De informatie verzonden in dit e-mail bericht is vertrouwelijk en is uitsluitend bestemd voor de geadresseerde van dit bericht. Lees verder: www.eur.nl/email-disclaimer <http://www.eur.nl/email-disclaimer> The information in this e-mail message is confidential and may be legally privileged. Read more: www.eur.nl/english/email-disclaimer<http://www.eur.nl/english/email-disclaimer> ________________________________ * * 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/