Announcement

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

  • Generating Monthly variable from Year and Month separate Variables.

    Greetings, I've reviewed the Statalist forum in order to seek an answer for my particular case. However, even when there are plenty of similar topics, I couldn't find my specific case to solve the problem.

    I got a data set where I extracted the Month and the Year (both numerical) as the next table shows.


    Code:
    Year    Month_Num
    1990    1
    1990    2
    1990    3
    1990    4
    1990    5
    1990    6

    I have tried combining Month_Num and Year into a single variable which is a string variable of the form:


    Code:
    Year    Month_Num    YM
    1990    1                   1-1990
    1990    2                   2-1990
    1990    3                   3-1990
    1990    4                   4-1990
    1990    5                   5-1990
    1990    6                   6-1990

    But when I use
    gen date=date(YM, "MY")
    format date %tm

    It produces results way strange,

    Code:
    Year    Month_Num    YM    date
    1990    1                  1-1990    2873m3
    1990    2                  2-1990    2875m10
    1990    3                  3-1990    2878m2
    1990    4                  4-1990    2880m9
    1990    5                  5-1990    2883m3
    1990    6                  6-1990    2885m10


    I know there should be an easy way by just using the Month_Num and Year variable and put them together and simply format it as time monthly. But I wasn't able to figure out a solution.

    Thank you for your time.

  • #2
    Well, shortly after making this post I've found the answer in the following old link:

    https://www.stata.com/statalist/arch.../msg01022.html

    The solution uses a function called ym, in my case it was this code as followed:

    Code:
    gen modate=ym(Year,Month_Num)
    format modate %tm
    Which as output brings this.

    Code:
    modate
    1990m1
    1990m2
    1990m3
    1990m4
    1990m5

    Comment


    • #3
      Your instinct is right: this is (or should be) an easy question, so start with help and/or search in Stata, not Googling.

      Here's a nudge:


      Code:
      help datetime
      is the portal to most of the basic stuff on dates and times. The kind of thing you were trying was not crazy but needs to be understood.

      Code:
      . di date("1-1990", "MY")
      10958
      
      . di %td date("1-1990", "MY")
      01jan1990
      The job of date() is to give a daily date as

      Code:
      help date()
      also tells you. So if you give it a month and year as you did, that's fine, and what you get is a daily date (for the first day of that month in that year). But because it's a daily date, the only format that makes sense is a daily date format. Giving it a monthly date format is telling Stata these numbers are monthly dates! 1 January 1990 as a daily date is numerically 10958 (days from 1 January 1960) but as a monthly date it is 10958 months from January 1960 -- and that's a long way into the future.

      You found the right answer -- in one of my posts -- but you're better off reading Stata's help than my posts.
      Last edited by Nick Cox; 21 Nov 2020, 12:17.

      Comment


      • #4
        Expanding on Nick's advice, Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher.

        After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

        All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        Comment

        Working...
        X