Announcement

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

  • Calculate age between two dates

    Hi there,

    I am hoping someone can help. I need to calculate the year and months between two dates as example below. I appreciate there are several posts on this topic, but I have not found one that gets my novice STATA brain past the different formats.

    Many thanks in advance for any help
    Michelle

    Click image for larger version

Name:	Screenshot 2023-06-19 at 10.46.20 am.png
Views:	1
Size:	29.0 KB
ID:	1717605

  • #2
    We ask you to help us to help you by not giving screenshots but by using dataex to show a data example we can use directly. Please review https://www.statalist.org/forums/help#stata and in passing https://www.statalist.org/forums/help#spelling.

    Here it seems that you have a datetime as a string variable, a month as another string variable, and a year as a numeric variable.

    So here is some technique but using more informative variable names. Stata has recently added many functions to do with date differences (including age and birthday problems) and if you are using Stata 18 (which is assumed unless you state otherwise) they are accessible to you. Here, however, I go back to first principles -- what you want is the difference between two monthly dates. The first can be calculated by looking at the first 7 characters of your longer string and the second can be calculated by combining a month and a year, with needed trickery to extract a substring and to convert the year on the fly to a string.

    So, the main workhorse here is monthly(). A serious question is how do I know this, and the answer is by looking at the help previously. There is no road to understanding dates and times that does not involve looking at

    Code:
    help datetime
    and following links. Skipping and skimming vigorously are allowed in any review.


    Code:
    . clear
    
    . set obs 1
    Number of observations (_N) was 0, now 1.
    
    . gen datetime = "2019-02-12T11:59:21.000Z"
    
    . gen month = "May"
    
    . gen year = 1956
    
    . list
    
         +-----------------------------------------+
         |                 datetime   month   year |
         |-----------------------------------------|
      1. | 2019-02-12T11:59:21.000Z     May   1956 |
         +-----------------------------------------+
    
    . gen wanted = monthly(substr(datetime, 1, 7), "YM") - monthly(strofreal(year) + month, "YM")
    
    . l
    
         +--------------------------------------------------+
         |                 datetime   month   year   wanted |
         |--------------------------------------------------|
      1. | 2019-02-12T11:59:21.000Z     May   1956      753 |
         +--------------------------------------------------+
    
    . di 753/12
    62.75
    The spot check is that someone born in May 1956 was indeed 62 years old in February 2019, and the calculation is supported even if your data aren't about people at all.

    Comment


    • #3
      Many thanks Nick, I'll give this a go.
      Apologies for the screenshot, noted.
      Kind regards
      Michelle

      Comment


      • #4
        Thank you so much, working perfectly and is correct for an individual born May 1956
        Kind regards,
        Michelle

        Comment

        Working...
        X