Announcement

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

  • How to Set Quarterly Data

    I am trying to do some VAR analysis, but I can't get the tsset correct, because of how the data in the date is structured. The data is integers, not strings. But I can't tsset for quarterly without it saying there are gaps, but there are no gaps. As a work around I tried to generate two variables for the year and the quarter, but thats where I'm stuck. The data is in integers, but is currently displayed like:

    DATE
    Jan 1, 1972
    Apr 1, 1972
    Jul 1, 1972
    Oct 1, 1972
    Jan 1, 1973

    I attached the file.
    Attached Files

  • #2
    If you have not yet imported your data into Stata, it is premature to ask for help with coding your analysis. If you have, then you should show an example from your Stata data set using the -dataex- command so that anyone who wants to help you can know exactly what you are starting with. As it is, there is no way to know if what you have there is a string variable that reads to human eyes like a date, or if you have a bona fide Stata internal format numeric variable with a datetime display format applied. The approach depends on that.

    If you are running version
    16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc
    install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save
    you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data
    that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also
    makes it possible for those who want to help you to create a faithful representation of your example to try out their
    code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Also, if you read the Forum FAQ before posting, as all are asked to do, you would know that attaching files is discouraged, and attaching spreadsheets is particularly deprecated. \

    With those preliminaries taken care of, if what you have is a string variable
    Code:
    gen real_date_variable = daily(DATE, "MDY")
    gen qdate = qofd(real_date)
    format real_date %td
    format qdate %tq
    tsset qdate, quarterly
    If your DATE variable is actually already a Stata internal format numeric date variable, then skip the first line of that code, and replace real_date by DATE in the -gen qdate = ...- command and drop the -format real_date %td- command.

    If you are going to be working with this kind of data on a regular basis, it behooves you to learn about Stata's datetime variables and the various functions for creating them and converting them into different types. Run -help datetime- and click on the blue link to the complete PDF manual entry near the top. Read that entire section. It's very long and detailed and you won't remember it all, but you will gain an understanding of how Stata works with dates and times, and a general sense of the different types of datetime variables you can use and how to convert form one type to another. You will then be able to use the help files for the details of the syntax and usage of the specific commands as need arises.

    Comment


    • #3
      Dates in stata are stored as integers, even though the displayed date is formatted as a string variable.

      You will have to create a new variable that extracts the quarter. See this thread: https://www.statalist.org/forums/for...o-year-quarter

      So you will have to do something like:

      generate datequarter=qofd(datevar)
      format datequarter %tq

      Comment


      • #4
        Dates in stata are stored as integers, even though the displayed date is formatted as a string variable.
        It would be more accurate to say that dates should be stored as integers in Stata data sets. But depending on the original source of the data and the data management used to create the Stata data set, it is not uncommon to end up with a Stata data set that contains a string variable that reads like a date. Such variables are pretty close to useless until they are converted to Stata integer date variables. But they pop up frequently in real world data sets.

        Comment


        • #5
          #3

          the displayed date is formatted as a string variable.
          '
          '
          More wrong than right, strictly speaking. Users see a character string in most contexts where a numeric data variable has been assigned a date display format, but such formats can only be assigned to numeric variables.

          Comment

          Working...
          X