Announcement

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

  • Date based on elapsed months

    I have been unable to find a solution to my problem.

    I have a date of birth of children and I now want to find the specific date of which they turn 60 months of age.
    How do I go about solving this?



  • #2
    Welcome to Statalist.

    In future, please try to follow the instruction on the FAQ (http://www.statalist.org/forums/help) and provide the sample data/format. Especially date because they come in so many formats and types. What I showed below assumes that it's been input as a date that Stata can understand. If that does not work, then we must see the data format.

    Code:
    clear
    input bdate
    22222
    end
    
    format bdate %td
    gen date6m = bdate + (365.25*5)
    format date6m %td
    Results:

    Code:
         +-----------------------+
         |     bdate      date6m |
         |-----------------------|
      1. | 03nov2020   03nov2025 |
         +-----------------------+
    Last edited by Ken Chui; 22 Apr 2022, 08:22.

    Comment


    • #3
      Thank you very much Ken! I am very new to Stata and the forums. I use the td format.
      It works great but I do not understand why some of them have a birthday that differs from the day they were born (sorry for the screenshot).



      Comment


      • #4
        Originally posted by Morten Hans Jensen View Post
        Thank you very much Ken! I am very new to Stata and the forums. I use the td format.
        It works great but I do not understand why some of them have a birthday that differs from the day they were born (sorry for the screenshot).


        If you just want the year to be increased by 5:

        Code:
        clear
        input bdate
        22222
        end
        
        format bdate %td
        gen date6m = mdy(month(bdate), day(bdate), year(bdate) + 5)
        format date6m %td

        Comment


        • #5
          Thank you!
          Works like a charm.

          Easy fix but not for a newbie!

          Comment


          • #6
            Recent versions of Stata include some new functions for working with relative dates and durations.
            Code:
            help datetime_relative_dates
            help datetime_durations
            They can be useful in cases like this, provided you don't take the function name "birthday" too seriously,
            Code:
            . describe when
            
            Variable      Storage   Display    Value
                name         type    format    label      Variable label
            ------------------------------------------------------------------------------------------------
            when            float   %td                   
            
            . list, clean
            
                        when  
              1.   04jul2020  
              2.   28feb2020  
              3.   29feb2020  
              4.   01mar2020  
            
            . generate plus5a = birthday(when,yofd(when)+5)
            
            . generate plus5b = mdy(month(when), day(when), year(when) + 5)
            (1 missing value generated)
            
            . format %td plus5a plus5b
            
            . list, clean
            
                        when      plus5a      plus5b  
              1.   04jul2020   04jul2025   04jul2025  
              2.   28feb2020   28feb2025   28feb2025  
              3.   29feb2020   01mar2025           .  
              4.   01mar2020   01mar2025   01mar2025  
            
            .

            Comment

            Working...
            X