Announcement

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

  • Subtract 9 months from each of the 10K+ dates in a column

    Hello, I've been trying to Subtract nine months from each of the 10K+ dates in a column and want to save it in another column. Could someone please help me with the code?

  • #2
    For column read variable.

    Are these monthly dates, daily dates, or something else?

    If they are daily dates, what do you want to happen if 9 months before is 29 February in a non-leap year?

    Comment


    • #3
      There are more problems possible. Check if I have all cases:

      31 Jan implies 31 April 9 months earlier

      31 Mar implies 31 Jun ditto

      31 Aug implies 31 Nov

      30 Nov implies 30 Feb

      29 Nov implies 29 Feb -- only in leap years

      Here is one fudge demonstrated:

      Code:
      clear 
      set obs 365 
      gen ddate = mdy(12, 31, 2021) + _n 
      format ddate %td 
      
      gen guess = dofm(mofd(ddate) - 9) + day(ddate) - 1 
      format guess %td 
      
      bysort guess (ddate) : gen N = _N 
      
      sort ddate guess 
      
      list ddate guess if N > 1 
      
           +-----------------------+
           |     ddate       guess |
           |-----------------------|
       31. | 31jan2022   01may2021 |
       32. | 01feb2022   01may2021 |
       90. | 31mar2022   01jul2021 |
       91. | 01apr2022   01jul2021 |
      243. | 31aug2022   01dec2021 |
           |-----------------------|
      244. | 01sep2022   01dec2021 |
      333. | 29nov2022   01mar2022 |
      334. | 30nov2022   02mar2022 |
      335. | 01dec2022   01mar2022 |
      336. | 02dec2022   02mar2022 |
           +-----------------------+

      Comment


      • #4
        Another rule would be to subtract 274 days which is the best approximation to 9/12 of a year over a 4 year cycle:


        Code:
        . mata
        : 4 * (273, 274, 275) / (3 * 365 + 366)
                         1             2             3
            +-------------------------------------------+
          1 |  .7474332649   .7501711157   .7529089665  |
            +-------------------------------------------+

        Comment

        Working...
        X