Announcement

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

  • Calculating Age from two variables

    Hello everyone,

    I have a dataset like this-
    input str4 hsn double cdob str10 bqdate_1
    "1004" 19251 " 4/16/2026"
    "1005" 19227 " 4/16/2011"
    "1007" 19235 " 4/16/2011"
    "1009" 19247 " 4/16/2013"
    "1013" 19244 " 4/16/2021"
    end
    format %td cdob

    cdob is the date of birth, and bqdate_1 is date of administering the data. Now I need to find out the age from the difference of these two variables. I know I need to change the format of these two variables, but I'm not sure exactly what to do for that. After changing the format I need to calculate differences between dates in months by using mofd prefix. Can someone help me to solve this problem? Thanks in advance.

    Fahmida


  • #2
    From you date format, it would appear that the dates of birth are during the year 2012. But that would lead to a couple of negative ages as of the dates in bqdate_1.

    Are you sure that the dates of birth aren't various months in the 1920s instead (although that would lead to centenarians)? You might want to go back to the data source and have things cleaned up first.
    Last edited by Joseph Coveney; 05 Feb 2019, 18:58.

    Comment


    • #3
      Hi Joseph,

      The dates of birth are correct, but there are problems in the date of administering the data (bqdate_1) variable. I've already informed my Professor about it. However, I need to know how to calculate the age from these two variables once I get the edited dataset, which I'm trying to figure out.

      Fahmida

      Comment


      • #4
        Try something like the following.
        Code:
        version 15.1
        
        clear *
        
        input str4 hsn double cdob str10 bqdate_1
        "1004" 19251 " 4/16/2026"
        "1005" 19227 " 4/16/2011"
        "1007" 19235 " 4/16/2011"
        "1009" 19247 " 4/16/2013"
        "1013" 19244 " 4/16/2021"
        end
        
        generate int bq_dt = date(bqdate_1, "MDY")
        
        format %tdCY-N-D cdob bq_dt
        
        generate int dob_m = mofd(cdob)
        generate int bq_m = mofd(bq_dt)
        format *_m %tm
        
        generate int age = bq_m - dob_m
        
        list, noobs separator(0) abbreviate(20)
        
        exit

        Comment

        Working...
        X