Announcement

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

  • How to generate a variable based on children's date of birth?

    Dear all,

    I want to create a is a dummy variable which is equal to 1 if the child was born in December and 0 if she/he was born in January of the following year. The data example below includes both year and month of birth of both mothers and their children. Any help would be highly appreciated.

    Code:
    clear
    input byte mom_mob int mom_yob byte child_mob int child_yob
     6 1982  1 2014
     5 1988 12 2017
     6 1989 12 2011
     5 1976 12 2003
     9 1976 12 2003
    11 1986  1 2015
     3 1976 12 2012
     3 1976  1 1998
    12 1972 12 1995
     2 1975 12 2002
     9 1970 12 1991
     4 1975 12 2000
     6 1969  1 1995
    11 1991 12 2014
    11 1985 12 2013
     5 1969 12 2008
     1 1993 12 2015
     3 1988  1 2013
     5 1983 12 2017
     5 1983  1 2006
     5 1983 12 2003
     8 1981 12 2006
     9 1976  1 1995
     7 1985 12 2013
     1 1970  1 2007
     4 1977 12 2000
     4 1968 12 2001
     4 1968 12 1994
     3 1980  1 2004
     7 1985 12 2010
    10 1968  1 1991
     2 1979  1 2008
     2 1979  1 2004
    11 1975 12 2006
    11 1975 12 2002
     3 1975 12 2005
     9 1977 12 2006
     9 1988  1 2015
     2 1986  1 2013
     4 1977 12 2007
     4 1977  1 2000
    11 1987 12 2009
     7 1979  1 1999
     6 1989 12 2013
     6 1969 12 1989
     4 1978  1 2011
     7 1981  1 2003
    10 1981  1 2010
     1 1990  1 2016
    11 1972  1 1998
     4 1987  1 2008
     6 1971  1 1992
    11 1972 12 2001
     4 1971 12 1996
     1 1983 12 2007
     6 1980  1 2002
     8 1983 12 2009
     2 1984  1 2007
     9 1984  1 2013
    12 1972 12 1997
    12 1972 12 1992
     6 1986  1 2013
     9 1997 12 2015
     5 1985  1 2010
     6 1976  1 2006
     7 1972 12 2007
     7 1972 12 2007
     7 1972 12 2002
     7 1972  1 2000
     7 1983  1 2011
     3 1980 12 2012
     9 1974  1 2001
    12 1984  1 2013
     8 1996 12 2014
     3 1971 12 1996
     9 1990 12 2012
     9 1990  1 2008
     6 1987  1 2010
    10 1971  1 1999
    10 1971  1 1997
     6 1971 12 1993
    11 1974 12 1995
     1 1982 12 2007
     2 1973  1 1996
     2 1969 12 1993
     6 1979 12 1998
     4 1968 12 1997
     4 1968 12 1991
     3 1993 12 2016
    10 1986 12 2014
     9 1985  1 2007
     4 1981  1 2011
     5 1983  1 2008
     5 1983 12 2004
     7 1977 12 2001
     4 1973 12 2000
     6 1995 12 2014
     2 1977 12 1998
     3 1972  1 2005
     2 1984 12 2016
    end
    
    lab var mom_mob "mom's month of birth"
    lab var mom_yob "mom's year of birth"
    lab var child_mob "child's month of birth"
    lab var child_yob "child's year of birth"

  • #2
    Code:
    gen byte december_baby = 12.child_mob

    Comment


    • #3
      Thank you Prof. Clyde for kind support as always. May I add a follow-up question that would it be possible to create another dummy identifying children of interest, which equals 1 for children born in December in a given year and 0 for children born in Jan in the next year using a general command approach? For example, 1 for children born in Dec 2001 and 0 for children born in Jan 2002. Please note that this is a specific example to demonstrate what I wanted to have, however, in my actual dataset years of birth of children could be different (e.g., Dec 2005 and Jan 2006 and so on), that is why I want to know a general command for different scenario. Thank you.

      Comment


      • #4
        I'm not sure what the more general situation you are asking about is. If you mean code that would work correctly in a data set where there were months other than December and January, I would do that as:
        Code:
        gen byte wanted = 1 if child_mob == 12
        replace wanted = 0 if child_mob == 1
        In this case, the variable wanted would have missing values for any birth month other than December or January.

        But as long as all of the birth months are either 12 or 1, the code shown in #2 is simpler.

        Comment


        • #5
          Thank you, Professor.

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            I'm not sure what the more general situation you are asking about is. If you mean code that would work correctly in a data set where there were months other than December and January, I would do that as:
            Code:
            gen byte wanted = 1 if child_mob == 12
            replace wanted = 0 if child_mob == 1
            In this case, the variable wanted would have missing values for any birth month other than December or January.

            But as long as all of the birth months are either 12 or 1, the code shown in #2 is simpler.
            Dear Prof. Clyde,

            I have a similar question, but in a different context. Specifically, I want to create a variable called eligibility, in which individuals born just before a specific cutoff date are eligible for a policy benefit given a critical age, whereas those born just after the cutoff date are not. The cutoff dates and corresponding critical ages are April 1941 for age 60, April 1943 for age 61, April 1945 for age 62, April 1947 for age 63, and April 1949 for age 64. I would greatly appreciate it if you could help me out in this case.

            Thank you.

            Data
            Code:
            clear
            input float(yob mob)
            1949 4
            1949 4
            1949 4
            1943 5
            1943 5
            1940 6
            1940 6
            1950 2
            1950 2
            1950 2
            1950 2
            1949 2
            1949 2
            1949 2
            1949 2
            1941 3
            1941 3
            1941 3
            1941 6
            1941 6
            1941 6
            1941 6
            1945 6
            1945 6
            1947 2
            1947 2
            1947 2
            1945 6
            1945 6
            1940 4
            1940 4
            1940 4
            1940 4
            1941 3
            1941 3
            1948 2
            1948 2
            1948 2
            1948 2
            1944 6
            1944 6
            1947 2
            1947 2
            1943 3
            1943 3
            1940 3
            1940 3
            1944 6
            1944 6
            1944 6
            1944 6
            1943 3
            1943 3
            1946 4
            1946 4
            1946 4
            1942 4
            1942 4
            1942 4
            1943 5
            1943 5
            1943 5
            1943 5
            1942 6
            1942 6
            1942 6
            1942 6
            1946 3
            1946 3
            1946 3
            1946 3
            1950 3
            1950 3
            1950 3
            1950 3
            1946 2
            1946 2
            1946 2
            1946 2
            1944 5
            1944 5
            1942 5
            1942 5
            1942 5
            1942 5
            1949 5
            1949 5
            1949 5
            1949 5
            1944 4
            1944 4
            1944 4
            1948 3
            1948 3
            1948 4
            1948 4
            1948 4
            1948 4
            1944 3
            1944 3
            end

            Comment


            • #7
              I've never seen this kind of expression before, with a number and "." prefixed to a variable name, which appeared in #2 above:
              Code:
              gen byte december_baby = 12.child_mob
              Where should I look to find documentation about the usage and meaning of this construction? I'm drawing a blank.

              Comment


              • #8
                Mike Lacy examples are given under "selecting levels" in "help fvvarlist"

                Comment


                • #9
                  Re #6:
                  Code:
                  gen mdate = ym(yob, mob)
                  assert missing(mdate) == missing(mob, yob)
                  format mdate %tm
                  
                  gen delta = year(dofm(mdate - 4)) - 1941
                  gen eligibility_age = min(60 + floor(delta/2), 64) if inrange(yob, 1941, .)
                  This code is not very transparent. The alternative is to use a -gen- followed by a bunch of -replace- statements each conditioned on mdate being within a designated range. If there were only two or three changing cutoffs, I would do it that way. But when you have 5 cutoffs, it becomes a bit tedious and error-prone writing all of that out. Different programmers would draw the line between the two approaches in different places. It's a tradeoff between compactness/convenience and transparency.

                  Comment


                  • #10
                    Re #8: Aha, good point. I think of factor variable notation only as a way to specify predictors in a model.

                    Comment


                    • #11
                      Originally posted by Clyde Schechter View Post
                      Code:
                      gen byte december_baby = 12.child_mob
                      Dear Prof. Clyde,

                      I am sorry to bring up this topic again, however, I have a question similar to #1 but now I want to clear a running variable for my regression discontinuity regression based on individual's day and month of birth. Specifically, I want to create a variable such that those born exactly on or after June 12 will take on zero and positive values and those born before June 12 will take on negative values (e.g., values of that variable will look like: -2 -1 0 1 2 ... please note that zero would be those born on June 12). How can I do that with the following example? Thank you.

                      Code:
                      clear
                      input byte(day month) int year
                       4  4 2011
                      31  5 2013
                      29  3 2011
                      30  3 2012
                       8  4 2011
                      29  6 2012
                       9 10 2012
                      19  5 2009
                       5  5 2011
                       3  9 2012
                       6  2 2009
                      28 12 2012
                      21  1 2012
                      10  5 2009
                      13 10 2010
                      25 10 2009
                       8  9 2009
                      15  8 2011
                      29  9 2009
                      24  6 2011
                      27  4 2009
                       8  6 2012
                      10  4 2009
                       2 11 2013
                      23  7 2011
                      10 10 2013
                       8  1 2010
                      28  8 2009
                      11 11 2010
                      20  9 2009
                      19  5 2010
                      13  4 2010
                       1  7 2011
                      20 10 2013
                      23  9 2012
                      12  9 2010
                      26  8 2013
                      24  2 2013
                      28  8 2011
                       5 11 2012
                      18  9 2011
                      26  7 2013
                       9  8 2010
                       4  1 2012
                      20  6 2012
                      21  1 2010
                      12 11 2010
                      17  4 2009
                      14  9 2010
                      12  6 2010
                      26 10 2012
                      25 11 2009
                       8  9 2012
                       3 11 2009
                      30 11 2013
                      12  9 2009
                      26  9 2010
                      24 10 2012
                      25  7 2009
                      16  8 2013
                      14  6 2013
                      27  3 2013
                       8  8 2011
                      15  6 2009
                      11  5 2010
                      11  5 2010
                       2  9 2009
                      31  3 2009
                       4  8 2013
                      27  9 2013
                      15  3 2012
                      29  6 2012
                       3 11 2012
                      24  9 2012
                       3  2 2010
                      27 10 2010
                      10  6 2011
                      23  7 2010
                       9 12 2013
                      27 12 2013
                      10 10 2010
                       4  6 2011
                       3 11 2010
                      25 12 2011
                       5  7 2013
                      29  8 2009
                       8  3 2010
                       9  3 2011
                       1  1 2012
                       4  5 2012
                       1 11 2009
                      12 11 2009
                       9  9 2011
                      13 11 2013
                       1  8 2009
                      10  5 2012
                      14 12 2012
                      15  8 2010
                      12  2 2010
                      14  3 2013
                      end
                      label values day ag1d
                      label values month ag1m
                      label values year ag1y

                      Comment


                      • #12
                        Code:
                        clear
                        input byte(day month) int year
                         4  4 2011
                        31  5 2013
                        29  3 2011
                        30  3 2012
                         8  4 2011
                        29  6 2012
                         9 10 2012
                        19  5 2009
                         5  5 2011
                         3  9 2012
                         6  2 2009
                        28 12 2012
                        21  1 2012
                        10  5 2009
                        13 10 2010
                        25 10 2009
                         8  9 2009
                        15  8 2011
                        29  9 2009
                        24  6 2011
                        27  4 2009
                         8  6 2012
                        10  4 2009
                         2 11 2013
                        23  7 2011
                        10 10 2013
                         8  1 2010
                        28  8 2009
                        11 11 2010
                        20  9 2009
                        19  5 2010
                        13  4 2010
                         1  7 2011
                        20 10 2013
                        23  9 2012
                        12  9 2010
                        26  8 2013
                        24  2 2013
                        28  8 2011
                         5 11 2012
                        18  9 2011
                        26  7 2013
                         9  8 2010
                         4  1 2012
                        20  6 2012
                        21  1 2010
                        12 11 2010
                        17  4 2009
                        14  9 2010
                        12  6 2010
                        26 10 2012
                        25 11 2009
                         8  9 2012
                         3 11 2009
                        30 11 2013
                        12  9 2009
                        26  9 2010
                        24 10 2012
                        25  7 2009
                        16  8 2013
                        14  6 2013
                        27  3 2013
                         8  8 2011
                        15  6 2009
                        11  5 2010
                        11  5 2010
                         2  9 2009
                        31  3 2009
                         4  8 2013
                        27  9 2013
                        15  3 2012
                        29  6 2012
                         3 11 2012
                        24  9 2012
                         3  2 2010
                        27 10 2010
                        10  6 2011
                        23  7 2010
                         9 12 2013
                        27 12 2013
                        10 10 2010
                         4  6 2011
                         3 11 2010
                        25 12 2011
                         5  7 2013
                        29  8 2009
                         8  3 2010
                         9  3 2011
                         1  1 2012
                         4  5 2012
                         1 11 2009
                        12 11 2009
                         9  9 2011
                        13 11 2013
                         1  8 2009
                        10  5 2012
                        14 12 2012
                        15  8 2010
                        12  2 2010
                        14  3 2013
                        end
                        label values day ag1d
                        label values month ag1m
                        label values year ag1y
                        
                        gen ddate = mdy(month, day, year)
                        format ddate %td 
                        gen wanted = doy(ddate) - doy(mdy(6, 12, year))
                        sort wanted 
                        
                        list   
                        
                             +-----------------------------------------+
                             | day   month   year       ddate   wanted |
                             |-----------------------------------------|
                          1. |   1       1   2012   01jan2012     -163 |
                          2. |   4       1   2012   04jan2012     -160 |
                          3. |   8       1   2010   08jan2010     -155 |
                          4. |  21       1   2012   21jan2012     -143 |
                          5. |  21       1   2010   21jan2010     -142 |
                             |-----------------------------------------|
                          6. |   3       2   2010   03feb2010     -129 |
                          7. |   6       2   2009   06feb2009     -126 |
                          8. |  12       2   2010   12feb2010     -120 |
                          9. |  24       2   2013   24feb2013     -108 |
                         10. |   8       3   2010   08mar2010      -96 |
                             |-----------------------------------------|
                         11. |   9       3   2011   09mar2011      -95 |
                         12. |  14       3   2013   14mar2013      -90 |
                         13. |  15       3   2012   15mar2012      -89 |
                         14. |  27       3   2013   27mar2013      -77 |
                         15. |  29       3   2011   29mar2011      -75 |
                             |-----------------------------------------|
                         16. |  30       3   2012   30mar2012      -74 |
                         17. |  31       3   2009   31mar2009      -73 |
                         18. |   4       4   2011   04apr2011      -69 |
                         19. |   8       4   2011   08apr2011      -65 |
                         20. |  10       4   2009   10apr2009      -63 |
                             |-----------------------------------------|
                         21. |  13       4   2010   13apr2010      -60 |
                         22. |  17       4   2009   17apr2009      -56 |
                         23. |  27       4   2009   27apr2009      -46 |
                         24. |   4       5   2012   04may2012      -39 |
                         25. |   5       5   2011   05may2011      -38 |
                             |-----------------------------------------|
                         26. |  10       5   2012   10may2012      -33 |
                         27. |  10       5   2009   10may2009      -33 |
                         28. |  11       5   2010   11may2010      -32 |
                         29. |  11       5   2010   11may2010      -32 |
                         30. |  19       5   2010   19may2010      -24 |
                             |-----------------------------------------|
                         31. |  19       5   2009   19may2009      -24 |
                         32. |  31       5   2013   31may2013      -12 |
                         33. |   4       6   2011   04jun2011       -8 |
                         34. |   8       6   2012   08jun2012       -4 |
                         35. |  10       6   2011   10jun2011       -2 |
                             |-----------------------------------------|
                         36. |  12       6   2010   12jun2010        0 |
                         37. |  14       6   2013   14jun2013        2 |
                         38. |  15       6   2009   15jun2009        3 |
                         39. |  20       6   2012   20jun2012        8 |
                         40. |  24       6   2011   24jun2011       12 |
                             |-----------------------------------------|
                         41. |  29       6   2012   29jun2012       17 |
                         42. |  29       6   2012   29jun2012       17 |
                         43. |   1       7   2011   01jul2011       19 |
                         44. |   5       7   2013   05jul2013       23 |
                         45. |  23       7   2011   23jul2011       41 |
                             |-----------------------------------------|
                         46. |  23       7   2010   23jul2010       41 |
                         47. |  25       7   2009   25jul2009       43 |
                         48. |  26       7   2013   26jul2013       44 |
                         49. |   1       8   2009   01aug2009       50 |
                         50. |   4       8   2013   04aug2013       53 |
                             |-----------------------------------------|
                         51. |   8       8   2011   08aug2011       57 |
                         52. |   9       8   2010   09aug2010       58 |
                         53. |  15       8   2011   15aug2011       64 |
                         54. |  15       8   2010   15aug2010       64 |
                         55. |  16       8   2013   16aug2013       65 |
                             |-----------------------------------------|
                         56. |  26       8   2013   26aug2013       75 |
                         57. |  28       8   2011   28aug2011       77 |
                         58. |  28       8   2009   28aug2009       77 |
                         59. |  29       8   2009   29aug2009       78 |
                         60. |   2       9   2009   02sep2009       82 |
                             |-----------------------------------------|
                         61. |   3       9   2012   03sep2012       83 |
                         62. |   8       9   2009   08sep2009       88 |
                         63. |   8       9   2012   08sep2012       88 |
                         64. |   9       9   2011   09sep2011       89 |
                         65. |  12       9   2010   12sep2010       92 |
                             |-----------------------------------------|
                         66. |  12       9   2009   12sep2009       92 |
                         67. |  14       9   2010   14sep2010       94 |
                         68. |  18       9   2011   18sep2011       98 |
                         69. |  20       9   2009   20sep2009      100 |
                         70. |  23       9   2012   23sep2012      103 |
                             |-----------------------------------------|
                         71. |  24       9   2012   24sep2012      104 |
                         72. |  26       9   2010   26sep2010      106 |
                         73. |  27       9   2013   27sep2013      107 |
                         74. |  29       9   2009   29sep2009      109 |
                         75. |   9      10   2012   09oct2012      119 |
                             |-----------------------------------------|
                         76. |  10      10   2010   10oct2010      120 |
                         77. |  10      10   2013   10oct2013      120 |
                         78. |  13      10   2010   13oct2010      123 |
                         79. |  20      10   2013   20oct2013      130 |
                         80. |  24      10   2012   24oct2012      134 |
                             |-----------------------------------------|
                         81. |  25      10   2009   25oct2009      135 |
                         82. |  26      10   2012   26oct2012      136 |
                         83. |  27      10   2010   27oct2010      137 |
                         84. |   1      11   2009   01nov2009      142 |
                         85. |   2      11   2013   02nov2013      143 |
                             |-----------------------------------------|
                         86. |   3      11   2012   03nov2012      144 |
                         87. |   3      11   2010   03nov2010      144 |
                         88. |   3      11   2009   03nov2009      144 |
                         89. |   5      11   2012   05nov2012      146 |
                         90. |  11      11   2010   11nov2010      152 |
                             |-----------------------------------------|
                         91. |  12      11   2010   12nov2010      153 |
                         92. |  12      11   2009   12nov2009      153 |
                         93. |  13      11   2013   13nov2013      154 |
                         94. |  25      11   2009   25nov2009      166 |
                         95. |  30      11   2013   30nov2013      171 |
                             |-----------------------------------------|
                         96. |   9      12   2013   09dec2013      180 |
                         97. |  14      12   2012   14dec2012      185 |
                         98. |  25      12   2011   25dec2011      196 |
                         99. |  27      12   2013   27dec2013      198 |
                        100. |  28      12   2012   28dec2012      199 |
                             +-----------------------------------------+

                        Comment


                        • #13
                          Thank you Nick for the help. It works!

                          Comment

                          Working...
                          X