Announcement

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

  • Extract year from quarterly dates

    Hi,

    Seems like a very basic question but I can find the basic function that extract the year from quarterly dates

    Here is what I get when I try:

    Code:
    gen year = year(qyear)
    Code:
    qyear    year
    1986q2    1960
    1986q3    1960
    1986q4    1960
    1987q1    1960
    1987q2    1960
    1987q3    1960
    1987q4    1960
    1988q1    1960
    1988q2    1960
    1988q3    1960
    1988q4    1960
    1989q1    1960
    1989q2    1960
    1989q3    1960
    1989q4    1960
    1990q1    1960
    1990q2    1960
    1990q3    1960
    1990q4    1960
    1991q1    1960
    1991q2    1960
    1991q3    1960
    1991q4    1960
    1992q1    1960
    1992q2    1960
    1992q3    1960
    and I understand why...

    I would have liked to use one of the commands "qofd" but I could not find the of for quarterly date ("invalid syntaxt"):
    Code:
    gen year = yofq(qyear)
    Is there any way beside converting to string and extract the date from string? Thanks!

  • #2
    Please use dataex. That will allow people to see the current format of your qyear variable, which matters for what the correct answer is. It also allows people to copy paste (the above cant).
    See also the explanation in the FAQ (http://www.statalist.org/forums/help#stata).
    Code:
      ssc install dataex
      dataex in 1/20

    Comment


    • #3
      Please use dataex (SSC) to give data examples.

      This is all documented at help dates or http://www.stata.com/help.cgi?dates: you just to have read carefully!

      What are displayed in your example as e.g. 1986q2 are quarterly dates. At first sight they might be strings too, but your syntax would not then work.

      I will use display with examples to which anyone should be able to work out an answer independently.

      With the exception of calendar years, Stata uses 0 as origin for the first possible date in 1960.

      So 0 is


      Code:
      * when interpreted as a daily date, 1 January 1960 
      
      . di %td 0
      01jan1960
      
      * when interpreted as a a quarterly date, the first quarter of 1960 
      
      . di %tq 0
      1960q1

      With 0 as origin a little mental arithmetic shows that quarterly dates in the mid-1980s are numerically about 25 x 4 quarters from the origin. A precise example is that the second quarter of 1986 is

      Code:
      . di yq(1986, 2)
      105
      
      . di %tq  yq(1986, 2)
      1986q2
      year() is a function to extract the calendar year from daily dates.

      Hence it should not be a surprise that 1960 is returned for 105 as argument, as 105 is a daily date within 1960.


      Code:
      . di year(105)
      1960
      
      . di %td 105
      15apr1960
      This is documented: http://www.stata.com/help.cgi?year()

      Francois understands this, but it's also documented (by absence) that Stata doesn't provide all possible date-to-date conversion functions. Perhaps it should and then we wouldn't have to write answers like this.

      Hence you need to chain functions together: extract a daily date (strictly the beginning of each quarter) for each quarter, then extract a year:


      Code:
      . di yofd(dofq(105))
      1986

      Comment


      • #4
        Nick and Jorrit,

        Thanks for both answers. Nick's answer only work if you keep the same data sample since you input "105" in the answer. How would you code that in a way that I output the proper answer if I keep on changing the time series? For instance, I may run the same Stata code on a sample that start in 1979 without having the copy/paste the first date? Please let me know. Thanks!

        Comment


        • #5
          Sorry I responded too quickly. I can figure out the first year the following manner:

          di yofd(dofq(qyear)) However, would I use this command to generate "year" for the whole sample? Thanks

          Comment


          • #6
            Code:
            . list
            
                 +-------+
                 |  var1 |
                 |-------|
              1. |   105 |
              2. |  200  |
              3. | 12856 |
                 +-------+
            
            . gen year = yofd(dofq(var1))
            
            . list
            
                 +--------------+
                 |  var1   year |
                 |--------------|
              1. |   105   1986 |
              2. |   200   2010 |
              3. | 12856   5174 |
                 +--------------+
            that might not work for your data, depending on the current format. So again, please use dataex doing
            Code:
              ssc install dataex
              dataex in 1/20

            Comment


            • #7
              I didn't think I needed to explain how to use generate !!!

              Comment


              • #8
                Apologies. I'm not familiar with the command "di". I should have tried gen...!

                Comment


                • #9
                  I explained that I was using display:

                  I will use display with examples to which anyone should be able to work out an answer independently.
                  di is an abbreviation of display

                  Comment


                  • #10
                    Reply to #8
                    use the following command
                    Code:
                    gen year = yofd(dofq( qyear ))

                    Comment

                    Working...
                    X