Announcement

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

  • What is the best method / code of calculating respondents' ages?

    I have two variable: Q26 (month) and Q27 (year).
    Q26: What month were you born? The answer is 12 months. So there are 12 values for Q26.
    Q27: In what year were you born (yyyy)? The answer is
    1 1994
    2 1995
    3 1996
    4 1997
    5 1998
    6 1999
    7 Before 1994
    How to use these 2 variables to generate AGE variable? My survey was answered by undergraduate students in 2017. The goal is to do regression as the independent variable in the next step.

  • #2
    Code:
    gen year = 1993 + q27 if q27 < 7
    
    gen birth_month = ym(year, q26)
    format birth_month %tm
    
    //    CALCULATE AGE AS OF APRIL 2019
    gen age = (ym(2019, 4) - birth_month)/12
    Notes:
    1. You did not provide example data, so the code is not tested. Beware of errors.
    2. The result will be age in fractional years. If you want to truncate this to an integer, apply the -floor()- function to age.
    3. The result will be missing value for anyone who selected option 7 for Q27, as it is impossible to know the age when the year of birth is indeterminate.

    Comment


    • #3
      . label list q26
      q26:
      1 April
      2 August
      3 December
      4 February
      5 January
      6 July
      7 June
      8 March
      9 May
      10 November
      11 October
      12 September

      Do I need to recode q26 such that from 1 to 12 is in month order?

      Comment


      • #4
        Yes, definitely!

        Comment


        • #5
          I did that! THanks! Do you know how to attach my example data here?

          Comment


          • #6
            Use the -dataex- command. If you are running version 15.1 or a fully updated version 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.

            If you find it difficult to follow the instructions in -help dataex-, watch David Benson's video at https://youtu.be/bXfaRCAOPbI.

            Comment


            • #7
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input byte(q26 q27)
              10 4
               5 3
               8 4
              12 4
               7 4
               5 2
               9 4
              12 3
               4 3
               4 3
               6 4
               2 3
               5 4
               3 5
               5 5
               6 5
               8 5
               6 4
              12 4
               4 1
              12 4
               7 4
               9 4
               9 3
              11 2
              end
              label values q26 month
              label def month 2 "February", modify
              label def month 3 "March", modify
              label def month 4 "April", modify
              label def month 5 "May", modify
              label def month 6 "June", modify
              label def month 7 "July", modify
              label def month 8 "August", modify
              label def month 9 "September", modify
              label def month 10 "October", modify
              label def month 11 "November", modify
              label def month 12 "December", modify
              label values q27 q27
              label def q27 1 "1994", modify
              label def q27 2 "1995", modify
              label def q27 3 "1996", modify
              label def q27 4 "1997", modify
              label def q27 5 "1998", modify

              Comment


              • #8
                After running these two codes:
                gen year = 1993 + q27 if q27 < 7 gen birth_month = ym(year, q26)H How to interpret birth_month variable now?

                Comment


                • #9
                  Run the next one, -format birth_month %tm- and it will be obvious.

                  Comment

                  Working...
                  X