Announcement

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

  • Add/subtract days/month to a Stata date

    Hi,

    I would like to substract 1 months to a certain date
    Code:
    date
    28jan2016
    29jan2016
    01feb2016
    02feb2016
    03feb2016
    04feb2016
    05feb2016
    08feb2016
    09feb2016
    10feb2016
    11feb2016
    12feb2016
    I would like to create a variable "date1" that substract 1 month to date. Can anyone help? Thanks!


  • #2
    This isn't trivial. The previous month often has fewer days (true of March, May, June, etc.), so what should happen for e.g. 31mar2016?

    Comment


    • #3
      how can i subtract days then? I'd like to subtract 30 days. Thanks

      Comment


      • #4
        Code:
        gen wanted = whatever - 30

        Comment


        • #5
          Easier than I thought. Thanks!

          Comment


          • #6
            I'm circling back on an old discussion. I would like to subtract or add months to a daily date in way that the day remains unchanged unless this is the end of the month, in which case, I would like to create a date with the last day of that month.

            For instance if I want to subtract 1 month:
            feb 11st 2020 becomes Jan 11st 2020
            and
            March 31st becomes Feb 28th in non leap years and Feb 29th in leap years.

            Please advise. Thanks.

            Comment


            • #7
              Originally posted by Francois Durant View Post
              I'm circling back on an old discussion. I would like to subtract or add months to a daily date in way that the day remains unchanged unless this is the end of the month, in which case, I would like to create a date with the last day of that month.

              For instance if I want to subtract 1 month:
              feb 11st 2020 becomes Jan 11st 2020
              and
              March 31st becomes Feb 28th in non leap years and Feb 29th in leap years.

              Please advise. Thanks.
              HI Francois,

              I think -egenmore- package has -eomd- command useful for your question. Try to install this package and use example command
              Code:
               egen neededvar = eomd(date), f(%dd_m_y) lag(1)
              .

              Goodluck,
              Cu,

              Comment


              • #8
                Elegant and efficient solution, exactly what I needed. Thanks a lot cu dao huy .

                Comment


                • #9
                  Note also

                  The Stata Journal (2019) 19, Number 3, pp. 719–728

                  DOI: 10.1177/1536867X19874247

                  Speaking Stata: The last day of the month

                  Nicholas J. Cox

                  Abstract. I discuss three related problems about getting the last day of the month in a new variable. Commentary ranges from the specifics of date and other functions to some generalities on developing code. Modular arithmetic belongs in every Stata user’s coding toolbox.

                  Keywords: dm0100, dates, days, weeks, months, functions, modulus, remainders, rotations

                  Comment


                  • #10
                    Thanks for bringing that up, I will take a look.

                    Comment


                    • #11
                      Hi all,

                      Adding again to this old conversation to contribute my solution. My problem was that I have a set of intake dates, and I need to calculate target dates that are 3-, 6-, 9-, and 12-months after the intake date. This brute force solution worked for me and hopefully it will help others. Note, though, that I was extremely lucky that none of my intake dates fell on the 29th, 30th, or 31st, which would make this approach more complicated. If people have ideas for how to improve this code or how to account for days ending in 29-31, I would appreciate it!

                      Data:
                      Intake_Date
                      19jan2023
                      19jan2023
                      26jan2023
                      26jan2023
                      22feb2023
                      22feb2023
                      22feb2023
                      02mar2023
                      02mar2023
                      02mar2023
                      10mar2023
                      10mar2023
                      10mar2023
                      10mar2023
                      16mar2023
                      16mar2023
                      16mar2023
                      16mar2023
                      28mar2023
                      28mar2023
                      28mar2023
                      28mar2023
                      04apr2023
                      04apr2023
                      25apr2023
                      25apr2023
                      26may2023
                      26may2023
                      02aug2023
                      02aug2023
                      02aug2023
                      02aug2023
                      04aug2023
                      13sep2023
                      13sep2023
                      13sep2023
                      08nov2023
                      21nov2023
                      21nov2023
                      09jan2024
                      09jan2024
                      09jan2024
                      18jan2024
                      18jan2024
                      18jan2024
                      15apr2024
                      15apr2024
                      02jul2024
                      15jul2024
                      Code to calculate 3-, 6-, 9-, and 12-months following intake date:
                      Code:
                      * adding in target survey dates
                      
                      gen month = month(Intake_Date)
                      gen day = day(Intake_Date)
                      gen year = year(Intake_Date)
                      
                      tab day
                      * latest day of intake is 28th - phew! If any youth intake on 29th, 30th, or 31st, additional adjustmnets are needed
                      
                      * 3 months first
                      
                      gen target_month3 = month+3
                      
                      * fix Oct, Nov, Dec
                      replace target_month3=1 if target_month3==13
                      replace target_month3=2 if target_month3==14
                      replace target_month3=3 if target_month3==15
                      
                      * also need to add a year
                      gen target_year3 = year + 1 if target_month3<=3
                      
                      gen target_survey_date3 = mdy(target_month3,day,year)
                      replace target_survey_date3 = mdy(target_month3,day,target_year3) if target_year3!=.
                      
                      format target_survey_date3 %td
                      
                      * now 6 months - add 3 months to target_month3 which is already clean
                      
                      gen target_month6 = target_month3+3
                      
                      * fix Oct, Nov, Dec
                      replace target_month6=1 if target_month6==13
                      replace target_month6=2 if target_month6==14
                      replace target_month6=3 if target_month6==15
                      
                      * also need to add a year
                      gen target_year6 = year + 1 if target_month6<=3 | target_year3!=.
                      
                      gen target_survey_date6 = mdy(target_month6,day,year)
                      replace target_survey_date6 = mdy(target_month6,day,target_year6) if target_year6!=.
                      
                      format target_survey_date6 %td
                      
                      * now 9 months - add 3 months to target_month6 which is already clean
                      
                      gen target_month9 = target_month6+3
                      
                      * fix Oct, Nov, Dec
                      replace target_month9=1 if target_month9==13
                      replace target_month9=2 if target_month9==14
                      replace target_month9=3 if target_month9==15
                      
                      * also need to add a year
                      gen target_year9 = year + 1 if target_month9<=3 | target_year6!=.
                      
                      gen target_survey_date9 = mdy(target_month9,day,year)
                      replace target_survey_date9 = mdy(target_month9,day,target_year9) if target_year9!=.
                      
                      format target_survey_date9 %td
                      
                      * for 12 months, just add 1 to year
                      
                      gen target_year12 = year + 1
                      
                      gen target_survey_date12 = mdy(month,day,target_year12)
                      
                      format target_survey_date12 %td
                      
                      * Let's reorder to put target dates at end
                      
                      order target_survey_date3 target_survey_date6 target_survey_date9 target_survey_date12, after(target_year12)
                      
                      * for sanity's sake, get difference between intake and survey dates
                      gen difference3 = target_survey_date3 - Intake_Date
                      gen difference6 = target_survey_date6 - Intake_Date
                      gen difference9 = target_survey_date9 - Intake_Date
                      gen difference12 = target_survey_date12 - Intake_Date
                      
                      sum difference*
                      * everything looks reasonable
                      Last edited by Rebecca Frausel; 05 Nov 2024, 15:24.

                      Comment

                      Working...
                      X