Announcement

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

  • Generating a date variable that is 9 moths after an existing date variable in my dataset

    Hello,

    I have a dataset that includes a certain date variable and I want to create another date variable in way that the new variable will be 9 months after the original variable. Example:

    var newvar
    01aug2021 01may2022
    12dec2021 12sep2022

    Any idea which commands can I use to apply this?

    Thanks in advance.
    Last edited by Noha Hassan; 14 Jun 2022, 04:53.

  • #2
    It depends how you define months. If it is 30 days, you add 30*9 to the original variable as Stata dates are in units of days. Your example corresponds to wanted2 below, and the implied duration is 8 months.

    Code:
    set obs 2
    g var= cond(_n==1, td(01aug2021), td(12dec2021))
    *MONTH= 30 DAYS
    g wanted1= var+ (30*8)
    *MONTH AS DEFINED IN #1
    g wanted2= mdy(cond(month(var)<=4, month(var)+8, month(var)+8-12), day(var), cond(month(var)<=4, year(var), year(var)+1))
    format var wanted? %td
    l
    Res.:

    Code:
    . l
    
         +-----------------------------------+
         |       var     wanted1     wanted2 |
         |-----------------------------------|
      1. | 01aug2021   29mar2022   01apr2022 |
      2. | 12dec2021   09aug2022   12aug2022 |
         +-----------------------------------+

    Comment


    • #3
      Note that a different definition -- same day of the month but 9 months later -- is problem-free only if this month and that 9 months later have the same number of days, ,
      which is true only for May,July and December.


      Code:
      . clear
      
      . set obs 12
      Number of observations (_N) was 0, now 12.
      
      . gen sandbox = mdy(_n + 1, 1, 2021) -1
      (1 missing value generated)
      
      . replace sandbox = mdy(1, 1, 2022) - 1 in L
      (1 real change made)
      
      .
      . gen checking = cond(_n <= 3, mdy(_n + 9, 1, 2021) - 1, mdy(_n - 3, 1, 2022) - 1)
      
      . format checking sandbox %td
      
      . list, sep(3)
      
           +-----------------------+
           |   sandbox   checking  |
           |-----------------------|
        1. | 31jan2021   30sep2021 |
        2. | 28feb2021   31oct2021 |
        3. | 31mar2021   30nov2021 |
           |-----------------------|
        4. | 30apr2021   31dec2021 |
        5. | 31may2021   31jan2022 |
        6. | 30jun2021   28feb2022 |
           |-----------------------|
        7. | 31jul2021   31mar2022 |
        8. | 31aug2021   30apr2022 |
        9. | 30sep2021   31may2022 |
           |-----------------------|
       10. | 31oct2021   30jun2022 |
       11. | 30nov2021   31jul2022 |
       12. | 31dec2021   31aug2022 |
           +-----------------------+

      Comment


      • #4
        Thank you so much, Andrew.
        Thank you so much, Nick

        That was really helpful!

        Comment

        Working...
        X