Announcement

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

  • Generating max(var) after a certain wave

    I would like to create a variable that is the maximum value, but not have it take over the entire variable for all waves.

    Here is what I'm dealing with specifically: I am interested in what happens to my DV if a child's mother gets divorced. Divorced=1, married=0. So once a mother divorces I want to keep that set to 1 for the waves following, even if she remarries.
    Let's say I have a mother who is married from 1993-1994 (divorced=0). In 1995, she is divorced (divorced=1). In 1996 and 1997, she is married again (divorced=0).
    93: 0
    94: 0
    95: 1
    96: 0
    97: 0
    I would like the 0 in 96, 97 and from there on out to stay a 1 since she has been divorced and her child experienced that divorce, but do not want to overwrite the 0's from 92-94.

    Right now I am using a max(var) syntax: egen max_div=max(divorced), by (child_id)
    But that gives me 1's for all waves (and while there is certainly value of knowing the marriage was on the rocks and seeing the impact that has on the child, that is not my focus right now).

    How can I keep the maximum value of a variable after it becomes the maximum value (in this case 1), but not overwrite the prior maximum value (in this case 0)?

    Thanks!


  • #2
    Perhaps this will start you in a useful direction.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int (child_id year divorced)
    101 93 0
    101 94 0
    101 95 1
    101 96 0
    101 97 0
    102 93 0
    102 94 1
    102 95 0
    102 96 1
    102 97 0
    end
    by child_id (year), sort: generate wanted = sum(divorced)
    replace wanted = 1 if wanted>1
    list, sepby(child_id)
    Code:
    . list, sepby(child_id)
    
         +-------------------------------------+
         | child_id   year   divorced   wanted |
         |-------------------------------------|
      1. |      101     93          0        0 |
      2. |      101     94          0        0 |
      3. |      101     95          1        1 |
      4. |      101     96          0        1 |
      5. |      101     97          0        1 |
         |-------------------------------------|
      6. |      102     93          0        0 |
      7. |      102     94          1        1 |
      8. |      102     95          0        1 |
      9. |      102     96          1        1 |
     10. |      102     97          0        1 |
         +-------------------------------------+

    Comment


    • #3
      Thank you, William, that was very helpful!

      Comment


      • #4
        Here's another way to do it:

        Code:
         
         by child_id (year), sort: generate wanted = sum(divorced) >= 1

        Comment


        • #5
          Thanks Nick! That worked perfectly.

          Comment

          Working...
          X