Announcement

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

  • Dropping first observations after threshold is reached

    Dear Statalist members,

    I am running panel data regression covering 32 years. My dependent variable is life satisfaction and I am analyzing the relationship between happiness and marital status.

    I want to show what is the difference between singles, married and the ones that marry later in life. For this I want to drop the first three years after somebody is married from he observations since they might bring bias (usually the happiest years in someones life). How can I do that ?

    Kind regards,
    Gabriela

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(lifesatis marstatus) long pid int(syear age)
     8 1 101 1987 57
     8 1 101 1988 58
     8 1 101 1989 59
     . . 101 1990  .
     . . 101 1991  .
     . . 101 1992  .
     . . 101 1993  .
     . . 101 1994  .
     . . 101 1995  .
     . . 101 1996  .
     . . 101 1997  .
     . . 101 1998  .
     . . 101 1999  .
     . . 101 2000  .
     . . 101 2001  .
     . . 101 2002  .
     . . 101 2003  .
     . . 101 2004  .
     . . 101 2005  .
     . . 101 2006  .
     . . 101 2007  .
     . . 101 2008  .
     . . 101 2009  .
     . . 101 2010  .
     . . 101 2011  .
     . . 101 2012  .
     . . 101 2013  .
     . . 101 2014  .
     . . 101 2015  .
     8 1 102 1984 44
     8 1 102 1985 45
     9 1 102 1986 46
     8 1 102 1987 47
     9 1 102 1988 48
    10 1 102 1989 49
     . . 102 1990  .
     . . 102 1991  .
     . . 102 1992  .
     . . 102 1993  .
     . . 102 1994  .
     . . 102 1995  .
     . . 102 1996  .
     . . 102 1997  .
     . . 102 1998  .
     . . 102 1999  .
     . . 102 2000  .
     . . 102 2001  .
     . . 102 2002  .
     . . 102 2003  .
     . . 102 2004  .
     . . 102 2005  .
     . . 102 2006  .
     . . 102 2007  .
     . . 102 2008  .
     . . 102 2009  .
     . . 102 2010  .
     . . 102 2011  .
     . . 102 2012  .
     . . 102 2013  .
     . . 102 2014  .
     . . 102 2015  .
     8 2 103 1984 21
     8 2 103 1985 22
     7 2 103 1986 23
     7 2 103 1987 24
     . . 103 1988  .
     . . 103 1989  .
     . . 103 1990  .
     . . 103 1991  .
     . . 103 1992  .
     . . 103 1993  .
     . . 103 1994  .
     . . 103 1995  .
     . . 103 1996  .
     . . 103 1997  .
     . . 103 1998  .
     . . 103 1999  .
     . . 103 2000  .
     . . 103 2001  .
     . . 103 2002  .
     . . 103 2003  .
     . . 103 2004  .
     . . 103 2005  .
     . . 103 2006  .
     . . 103 2007  .
     . . 103 2008  .
     . . 103 2009  .
     . . 103 2010  .
     . . 103 2011  .
     . . 103 2012  .
     . . 103 2013  .
     . . 103 2014  .
     . . 103 2015  .
    10 . 201 1984 58
    10 . 201 1985 59
     9 . 201 1986 60
    10 . 201 1987 61
    10 . 201 1988 62
     9 . 201 1989 63
     8 . 201 1990 64
    end
    label values lifesatis p11101
    label def p11101 10 "[10] Completely satisfied    10", modify
    label values marstatus d11104
    label def d11104 1 "[1] Married        1", modify
    label def d11104 2 "[2] Single         2", modify



  • #2
    Try this:
    Code:
    by pid (syear), sort: egen first_year_married = min(cond(marstatus == 1, ///
        syear, .))
        
    drop if inrange(syear, first_year_married, first_year_married+2) ///
        & !missing(first_year_married)
    Note: I'm not sure what "first three years after somebody is married" actually refers to. In the code above, I take it to mean that if somebody gets married in, say, 1987, we want to drop 1987, 1988, and 1989. But maybe you mean 1988, 1989, and 1990. Anyway, you can adjust the arguments of the -inrange()- function if what you mean isn't what I wrote.
    Last edited by Clyde Schechter; 02 Mar 2019, 09:14. Reason: Correct error in code.

    Comment


    • #3
      Dear Clyde,

      your assumption on the years I want to drop is absolutely correct. Thank you very much, deeply appreciated !

      Kind regards,
      Gabriela

      Comment


      • #4
        Please note also the StataCorp FAQs as recommended in our own FAQ Advice. How can I identify first and last occurrences systematically in panel data?

        https://www.stata.com/support/faqs/d...t-occurrences/

        That aside, do you have independent data for this statement [edited slightly]?

        first three years after somebody is married from the observations since they might bring bias (usually the happiest years in someone's life)
        Last edited by Nick Cox; 03 Mar 2019, 04:16.

        Comment

        Working...
        X