Announcement

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

  • create dummy variable based on condition of other variables

    Dear Statalist,

    I am struggling with this problem which is very difficult to explain. I am trying my best to describe it here and really appreciate if anyone can help me.

    My dataset based on a survey of the employment status of parents. It has 3 variables: id, year_of_survey, employment_status ( the variable "employment_status" is a dummy with two categories: 1= employed and 0= unemployed).
    I want to create another dummy variable called: "unemployment_experience" to define which mother/father has been through unemployment at the time she/he was interviewed (a mother/father once was unemployed will be considered to have experience in unemployment from then on even if he is employed after that). It should take value 1 if from the time interviewing backward, employment_status=0 at least once) and 0 otherwise. (1 = have experience in unemployment , 0 = no experience in unemployment)

    For example, I want to generate this following dataset:
    id year_of_survey employment_status unemployment_experience
    1 2000 1 0
    1 2001 1 0
    1 2002 0 1
    1 2003 1 1
    2 2000 1 0
    2 2001 1 0
    2 2002 1 0
    2 2003 0 1
    3 2000 0 1
    3 2001 1 1
    3 2002 1 1
    3 2003 1 1
    3 2004 1 1
    3 2005 1 1
    Thank you so much for your time.

    Best regards,
    Cameron.

  • #2
    You did not post your data in a form that lends itself to importation into Stata, so the following code is untested and may contain errors:

    Code:
    by id (year_of_survey), sort: gen unemployment_experience = min(sum(employment_status == 0), 1)
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 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
      Thank you so much for your advice and kind help, Clyde Schechter.
      Here is my data example:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long id int year_of_survey byte employment_status
       710904 1994 1
       710904 1995 1
       710904 1996 0
       710904 1997 0
         5902 1993 1
         5902 1994 1
         5902 1996 1
         5902 1997 1
         5902 1998 1
       923202 1993 1
       923202 1994 1
       923202 1996 0
       923202 1997 0
       923202 1998 0
       710904 1998 0
       710904 1999 0
       710904 2000 0
       710904 2001 0
         6001 1984 1
         6001 1985 1
         6001 1986 1
         6001 1987 1
         6001 1988 1
         6001 1989 1
         6001 1990 1
         6001 1991 1
         6001 1992 1
         6001 1993 1
         6001 1994 1
         6001 1995 1
         6001 1996 1
         6001 1997 1
         6001 1998 1
         6001 1999 1
         6002 1984 0
         6002 1985 0
         6002 1986 1
         6002 1987 1
         6002 1988 1
         6002 1989 1
         6002 1990 1
         6002 1991 1
         6002 1992 1
         6002 1993 1
         6002 1994 1
         6002 1995 1
         6002 1996 1
         6002 1997 1
         6002 1998 1
         6002 1999 1
         6002 2001 1
         6002 2002 1
         6002 2003 1
         6002 2004 0
         6002 2005 0
         6002 2006 1
         6002 2007 1
         6002 2008 1
         6002 2009 1
         6002 2010 1
         6002 2011 1
         6002 2012 1
         6002 2013 0
         6002 2014 0
         6002 2015 0
         6002 2016 0
         6003 1997 0
         6003 1998 0
         6003 1999 0
         6003 2001 0
         6003 2002 0
         6003 2003 1
         6001 2001 1
         6001 2002 1
         6001 2003 1
         6001 2004 1
         6001 2006 1
         6001 2007 1
         6003 2004 1
         6003 2005 1
         6003 2006 0
         6003 2007 1
         6003 2008 1
         6003 2009 1
         6003 2010 1
         6003 2011 1
         6003 2012 1
         6003 2013 1
         6003 2014 1
         6003 2016 1
      1208502 2007 0
      1208502 2008 1
      1208502 2009 1
      1208502 2010 1
      1208502 2011 1
      1208502 2012 1
      1208502 2013 1
      1208502 2014 1
      1208502 2016 1
      end
      label values employment_status e11102
      label def e11102 0 "[0] Not Employed   0", modify
      label def e11102 1 "[1] Employed       1", modify

      Comment


      • #4
        Thank you so much Clyde Schechter, you solved my problem!

        Comment

        Working...
        X