Announcement

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

  • Creating month-of-the-year dummies

    Hello everyone!

    I have a dataset with monthly data. My date variable is in the format of "YM", so, e.g. "1983m1" (type = float, format =%tm)
    Now, for each monthly observation, I want to create a month-of-the-year dummy for my regression.

    I already tried the following code from another fellow member of the forum:
    egen month =seq(), to (12)
    tab month, gen(m)

    Technically, this works perfectly fine. However, the problem is that sometimes, I have a month missing in my observation so that at one point, the month-of-the year dummies and my date variable are not running in sync anymore.

    Is there a way that I can create my dummies directly based on my date variable? So that e.g. if my date variable jumps from "1983m1" directly to "1983m3", my month-of-the year dummy would also jump from "1" to "3" ?

    Already thanks a lot for your time and support!

  • #2
    Hi Samuel,

    Can you not use gen month = month(date)?

    See this article for more - https://stats.idre.ucla.edu/stata/fa...ith-tm-format/

    Best,
    Rhys

    Comment


    • #3
      #2 Rhys Williams month() is for extracting the month from a daily date variable, so that if date is a monthly date variable the idea needs a tweak:


      Code:
      gen month = month(dofm(date))
      The page you cite does explain this and does it in two steps, which isn't essential.

      #1 The definition of a dummy is a little elastic, but a variable with values 1 ... 12 wouldn't typically qualify. Once you have one such, however, you don't need indicators or dummies for each month as specifying

      Code:
      i.month
      in a command usually is sufficient for Stata purposes.

      Comment


      • #4
        Hi Rhys,

        thanks for your quick and helpful response!

        Using gen month = month(date) didn't work directly...
        However, I followed the instructions in the article and included an intermediary step: I first converted my date variable using "dofm" so that e.g. "1983m1" is displayed as "8401". From then on, I was able to generate my month-of-the year dummies.

        Thanks a lot!

        Comment


        • #5
          See this FAQ here
          https://stats.idre.ucla.edu/stata/fa...ith-tm-format/
          , and if you do not figure it out yourself after you have read the FAQ, show a sample of your data using -dataex-.

          Comment


          • #6
            Dear Nick,

            thanks for the short-cut and the additional explanations!

            I am afraid I didn't understand what you meant with using the "i.month".

            From my month-variable, I would have now used "tab month, gen(m)" to generate my dummies.

            Then, I would have run a regression such as: "regress log_SVI m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12"

            Do you mean that there is a nicer way of doing this?

            Thanks in advance for your clarification!

            Samuel

            Comment


            • #7
              He is saying that since Stata 11 in all native Stata commands you can use factor variable notation, see
              Code:
              help fvvarlist
              The equivalent way to run the regression you have in mind using factor variable notation would be

              Code:
              regress log_SVI i.month
              It does not hurt that you know how to generate dummies manually, because some user written commands do not understand factor variable notation.

              Originally posted by Samuel Pfaff View Post
              Dear Nick,

              thanks for the short-cut and the additional explanations!

              I am afraid I didn't understand what you meant with using the "i.month".

              From my month-variable, I would have now used "tab month, gen(m)" to generate my dummies.

              Then, I would have run a regression such as: "regress log_SVI m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12"

              Do you mean that there is a nicer way of doing this?

              Thanks in advance for your clarification!

              Samuel

              Comment


              • #8
                thank you, now it is all clear to me!

                Comment


                • #9
                  A search can fail or disappoint by giving too few or too many hits . But

                  Code:
                  search dummy
                  points among other things to

                  https://www.stata.com/support/faqs/d...mmy-variables/

                  which both answers the question there and explains that often explicit dummy or indicator variables are not needed, as Joro Kolev explains.

                  Comment

                  Working...
                  X