Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate dummy variable for all years when an event continue

    Dear Stata users:

    I have a question regarding generating dummy variables for whether the events still continue. I have a panel dataset shown below. The variable "time" means how long this event continues. For instance, the first row denotes the event that happened to id==1 in 2000 and continues for 3 years.

    year id time dummy
    2000 1 3 1
    2001 1
    2002 1
    2003 1
    2004 1
    2005 1 2 1
    2006 1
    2007 1
    2008 1
    2009 1
    2010 1
    2000 2
    2001 2
    2002 2 5 1
    2003 2
    2004 2
    2005 2
    2006 2
    2007 2
    2008 2
    2009 2
    2010 2


    I want to generate a dummy to denote whether this event still continues to this year on this id. So, I want to replace the dummy variable with the following:

    year id time dummy
    2000 1 3 1
    2001 1 1
    2002 1 1
    2003 1
    2004 1
    2005 1 2 1
    2006 1 1
    2007 1
    2008 1
    2009 1
    2010 1
    2000 2
    2001 2
    2002 2 5 1
    2003 2 1
    2004 2 1
    2005 2 1
    2006 2 1
    2007 2
    2008 2
    2009 2
    2010 2

    How to code this please?



  • #2
    It is not a good practice in Stata to create an indicator ("dummy") variable with 1/missing coding. A 1/0 coding will work better with Stata commands, so that is what I have done here:
    Code:
    clear
    input int year byte(id time)
    2000 1 3
    2001 1 .
    2002 1 .
    2003 1 .
    2004 1 .
    2005 1 2
    2006 1 .
    2007 1 .
    2008 1 .
    2009 1 .
    2010 1 .
    2000 2 .
    2001 2 .
    2002 2 5
    2003 2 .
    2004 2 .
    2005 2 .
    2006 2 .
    2007 2 .
    2008 2 .
    2009 2 .
    2010 2 .
    end
    
    by id (year), sort: gen event_num = sum(!missing(time))
    by id event_num (year), sort: gen wanted = _n <= time[1] & event_num > 0
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      It is not a good practice in Stata to create an indicator ("dummy") variable with 1/missing coding. A 1/0 coding will work better with Stata commands, so that is what I have done here:
      Code:
      clear
      input int year byte(id time)
      2000 1 3
      2001 1 .
      2002 1 .
      2003 1 .
      2004 1 .
      2005 1 2
      2006 1 .
      2007 1 .
      2008 1 .
      2009 1 .
      2010 1 .
      2000 2 .
      2001 2 .
      2002 2 5
      2003 2 .
      2004 2 .
      2005 2 .
      2006 2 .
      2007 2 .
      2008 2 .
      2009 2 .
      2010 2 .
      end
      
      by id (year), sort: gen event_num = sum(!missing(time))
      by id event_num (year), sort: gen wanted = _n <= time[1] & event_num > 0
      In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
      Great! Thanks Mr. Schechter! It's very helpful! And I am using -dataex- command next time!

      Comment

      Working...
      X