Announcement

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

  • Generate new variable with the date from the previous year (Problem with leap years)

    Dear all,

    I want to create a new variable that has the date of the previous year in it:
    This is my code so far:

    gen leap = mod(year(FPEDATS), 4) == 0 & (mod(year(FPEDATS), 100) != 0 | mod(year(FPEDATS), 400) == 0)
    gen FPEDATSn_1 = FPEDATS - (365 + leap)
    format %td FPEDATSn_1

    but it doesn't work if the variable FPEDATS is in January and in a leap year as it then substracts one to many (e.g. out of 31jan2016 becomes 30jan2015)
    Also if the desired previous year FPEDATSn_1 is a leap year then out of e.g. 31jan2017 becomes 01feb2016

    Does someone know how to do it right?

    this is part of my data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long FPEDATS
    20850
    22311
    20850
    22311
    22339
    22704
    22311
    22311
    22311
    22311
    22311
    22311
    22311
    22704
    22311
    22311
    22311
    22339
    22704
    12084
    13545
    15006
    16467
    16467
    12084
    13545
    16467
    17928
    19389
    22311
    15006
    13545
    15006
    16467
    17928
    19389
    20850
    22311
    12084
    13208
    15034
    15006
    22311
    15006
    16467
    17928
    19389
    20850
    12084
    13545
    15006
    16467
    17928
    19389
    13545
    16467
    17928
    19389
    22311
    13545
    18686
    12842
    13573
    15034
    15399
    15764
    16130
    16495
    16860
    17225
    17591
    17956
    18321
    18686
    19052
    19417
    19782
    20878
    21974
    22339
    22704
    11747
    12112
    12842
    13208
    13573
    14303
    14669
    15034
    15399
    15764
    16130
    16495
    16860
    17225
    17591
    17956
    18321
    18686
    19052
    end
    format %td FPEDATS



    Thanks!

  • #2
    See if https://www.statalist.org/forums/for...ar-in-the-date solves your problem.

    Comment


    • #3
      thank you! but that still leaves me with one problem that i maybe didnt specify enough: If we have the date 28feb2009 then i need it for the previous year to be the last day of the month aswell which would be 29feb2008. How would i go about that? Identiffy that the wanted year is leap and that add 1 if the month is february?

      Comment


      • #4
        OK.

        The last day of the month is the day before the first day of the next month, always. No need to worry about leap years.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input long FPEDATS
        11747
        12084
        12112
        12842
        13208
        13545
        13573
        14303
        14669
        15006
        15034
        15399
        15764
        16130
        16467
        16495
        16860
        17225
        17591
        17928
        17956
        18321
        18686
        19052
        19389
        19417
        19782
        20850
        20878
        21974
        22311
        22339
        22704
        end
        format %td FPEDATS
        
        gen wanted = dofm(ym(year(FPEDATS) - 1, month(FPEDATS) + 1)) - 1
        
        format %td wanted
        
        list
        
             +-----------------------+
             |   FPEDATS      wanted |
             |-----------------------|
          1. | 29feb1992   28feb1991 |
          2. | 31jan1993   31jan1992 |
          3. | 28feb1993   29feb1992 |
          4. | 28feb1995   28feb1994 |
          5. | 29feb1996   28feb1995 |
             |-----------------------|
          6. | 31jan1997   31jan1996 |
          7. | 28feb1997   29feb1996 |
          8. | 28feb1999   28feb1998 |
          9. | 29feb2000   28feb1999 |
         10. | 31jan2001   31jan2000 |
             |-----------------------|
         11. | 28feb2001   29feb2000 |
         12. | 28feb2002   28feb2001 |
         13. | 28feb2003   28feb2002 |
         14. | 29feb2004   28feb2003 |
         15. | 31jan2005   31jan2004 |
             |-----------------------|
         16. | 28feb2005   29feb2004 |
         17. | 28feb2006   28feb2005 |
         18. | 28feb2007   28feb2006 |
         19. | 29feb2008   28feb2007 |
         20. | 31jan2009   31jan2008 |
             |-----------------------|
         21. | 28feb2009   29feb2008 |
         22. | 28feb2010   28feb2009 |
         23. | 28feb2011   28feb2010 |
         24. | 29feb2012   28feb2011 |
         25. | 31jan2013   31jan2012 |
             |-----------------------|
         26. | 28feb2013   29feb2012 |
         27. | 28feb2014   28feb2013 |
         28. | 31jan2017   31jan2016 |
         29. | 28feb2017   29feb2016 |
         30. | 29feb2020   28feb2019 |
             |-----------------------|
         31. | 31jan2021   31jan2020 |
         32. | 28feb2021   29feb2020 |
         33. | 28feb2022   28feb2021 |
             +-----------------------+
        See also

        SJ-19-3 dm0100 . . . . . . . . . . Speaking Stata: The last day of the month
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
        Q3/19 SJ 19(3):719--728 (no commands)
        discusses three related problems about getting the last day
        of the month in a new variable

        Stata now has a lastdayofmonth() function introduced fairly recently, but this solution works some versions back.

        Comment


        • #5
          Thank you although it looks like that doesnt work if the date is on the 31 of December unfortunately

          Comment


          • #6
            You're right. Sorry about that. Let's try again:

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input long FPEDATS
            19417
            19782
            20850
            20878
            21974
            22311
            22339
            22704
            23010
            end
            format %td FPEDATS
            
            gen wanted = dofm(mofd(FPEDATS) - 11) - 1
            
            format %td wanted 
            
            list 
            
                 +-----------------------+
                 |   FPEDATS      wanted |
                 |-----------------------|
              1. | 28feb2013   29feb2012 |
              2. | 28feb2014   28feb2013 |
              3. | 31jan2017   31jan2016 |
              4. | 28feb2017   29feb2016 |
              5. | 29feb2020   28feb2019 |
                 |-----------------------|
              6. | 31jan2021   31jan2020 |
              7. | 28feb2021   29feb2020 |
              8. | 28feb2022   28feb2021 |
              9. | 31dec2022   31dec2021 |
                 +-----------------------+

            Comment


            • #7
              Thanks a lot

              Comment

              Working...
              X