Announcement

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

  • Creating a time index

    My data currently has a period variable which is a number with the first 4 digits being the year, the second two being the month - e.g. Jan 2012 is 201201. These run from Jan 2012 - Dec 2015. Is there a way to create a new variable where these periods are numbered from 1 to 48?

  • #2
    See

    Code:
    help dates
    for guidance here. The easiest method I know using official commands is this:

    Code:
    clear
    
    input mydate
    201201
    201512
    end
    
    gen mdate = ym(floor(mydate/100), mod(mydate, 100))
    format mdate %tm
    
    list
    Note that the perhaps more obvious monthly(strofreal(mydate), "YM") does not work (as of Stata 14) with run-together integers. For the same reason, numdate (SSC) will not work either.

    The rather ancient todate (SSC) (first made public 2001) does work here. You must install it before you can use it, but need only do that once on the same system.

    Code:
    capture ssc install todate
    todate mydate, pattern(YYYYMM) gen(mdate2)

    Comment


    • #3
      I see the code works to change each period from 201201 to 2012m1 as appropriate. However I was hoping to simply number each period from 1 to 48. I.e. 201201 =1, 201202 = 2 etc. That said, I'm not really sure if this is even necessary - what would be best to use to denote time in panel data?

      Comment


      • #4
        You will want to follow the guidance in help datetime, which is without a doubt the most visited documentation on my system, with the second-most-visited being Chapter 24 (Working with dates and times) of the Stata User's Guide PDF available from the PDF Documentation item on Stata's Help menu. Before working with dates and times, any Stata user should read the very detailed Chapter 24 thoroughly. After that, the help datetime documentation will usually be enough to point the way.

        With regard to working with panel data, you should begin by familiarizing yourself with the introductory material in the introductory section ("xt") of the Stata Longitudinal-Data/Panel-Data Reference Manual PDF. In general to the extent that you have meaningful times, it is not a good idea to discard that information in favor of a sequence number.

        Comment


        • #5
          I agree with all of William's advice. But it follows from studying the help for dates as both he and I suggest that time indexes 1 up could be obtained from the monthly dates generated by just subtracting the constant ym(2011, 12). That shifts the origin in your case.

          For many of these little date problems I check my logic by using
          display for one-off calculations. di is allowed as an abbreviation.

          Code:
          . di ym(2012, 1)
          624
          
          . di ym(2011, 12)
          623
          This is just how you would do it: it is neither necessary nor helpful.
          Last edited by Nick Cox; 02 Sep 2016, 11:25.

          Comment

          Working...
          X