Announcement

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

  • Subtracting month from a date variable

    Im trying to find a way of subtracting a month from a date variable. Is there a simple way of doing this?
    Many thanks
    Rachel

  • #2
    Hi Rachel,

    Welcome to the Stata Forum.

    Yes, you may use the - month() - function.

    You may wish to take a look at examples. Here there is a FAQ exactly on this subject: http://www.ats.ucla.edu/stat/stata/f...s_ym2mandy.htm

    Hopefully that helps!

    Best,

    Marcos
    Last edited by Marcos Almeida; 09 Nov 2016, 08:26.
    Best regards,

    Marcos

    Comment


    • #3
      By subtracting perhaps you mean extracting. By date variable perhaps you mean daily date variable. Consider

      Code:
      . clear
      
      . set obs 1
      obs was 0, now 1
      
      . gen today = mdy(11, 9, 2016)
      
      . format today %td
      
      . gen month = month(today)
      
      . gen mdate = mofd(today)
      
      . format mdate %tm
      
      . list
      
           +-----------------------------+
           |     today   month     mdate |
           |-----------------------------|
        1. | 09nov2016      11   2016m11 |
           +-----------------------------+

      Comment


      • #4
        Many thank for your responses, but im looking for a way in which I can subtract one month away from an individual date. For example, if one observation is 11th June 1983, I want to create a variable that would give me 11th June 1983 - 1 month = 11th May 1983.

        Comment


        • #5
          By accident, you already have most of the ingredients for a solution, and following up with help datetime gives the others.

          The corresponding day in the previous month can be got in various ways, no doubt, but here is one, using the example from #3:

          Code:
          gen last = dofm(mofd(today) - 1) + day(today) - 1
          What's today's month?

          Code:
          mofd(today)
          What was the previous month?

          Code:
          mofd(today) - 1
          What was the first day of the previous month?

          Code:
          dofm(mofd(today) - 1)
          What was the corresponding day in the previous month?

          Code:
          dofm(mofd(today) - 1) + day(today) - 1
          This leaves untouched the question of what to do whenever the # day of each month follows a month with fewer days (can happen in March, April, June, September and November) for # = 29 or 30 or 31.






          Comment


          • #6
            Many thanks for this.
            R

            Comment


            • #7
              Another way is something like

              Code:
              mdy(cond(month(today) == 1, 12, month(today) - 1), day(today), cond(month(today) == 1, year(today) - 1, year(today) ) )
              perhaps better laid out like this

              Code:
              mdy(
              cond(month(today) == 1, 12, month(today) - 1),
              day(today),
              cond(month(today) == 1, year(today) - 1, year(today) )
              )

              Comment


              • #8
                for a more general discussion, you might want to look at the following: http://www.statalist.org/forums/foru...ounting-months

                Comment

                Working...
                X