Announcement

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

  • Creating time series

    Hi,

    The dataset that I am using has a 'month' variable in the format of '201507' to '201912'.
    I would like to create a time series data set so that I have a 'time' variable that starts with 201506 = 1 up to 201912 = 54.
    I tried
    Code:
     tsset month
    but got an error that there are repeated values.

    I think the other solution would be looping but I do not know how to formulate the codes for this.

    It would be great if I could get advice on this.

    Thank you very much!

  • #2
    First, your "month" variable is not suitable for use as a Stata Internal Format monthly date. While 201601 is the month after 201512, the difference is 89 rather than 1.

    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.

    However, that won't solve the problem that your data contains multiple observations with the same month, which is what tsset is telling you. A time series contains doesn't contain multiple observations at the same time. What do these repeated values mean in your data? How do you tell two observations in the same month apart?

    Comment


    • #3
      William Lisowski is bang on here. This is part of what you need, but the bigger deal is why you have duplicates. Perhaps you really have panel data.

      Code:
      clear 
      input baddate 
      201507 
      201912
      end 
      
      gen gooddate = ym(floor(baddate/100), mod(baddate, 100))
      format gooddate %tm 
      
      list 
           +--------------------+
           | baddate   gooddate |
           |--------------------|
        1. |  201507     2015m7 |
        2. |  201912    2019m12 |
           +--------------------+

      Comment


      • #4
        Thank you very much for your help!

        I think Nick's code 'gooddate' is the right form. and I also have panel data. I am sorry for the confusion.

        Also, will Stata think 2015m7 as the first time variable?

        Thank you again!

        Comment

        Working...
        X