Announcement

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

  • I can't extract quarters from a date

    Hey,
    I'm sorry to be bothering you with something that should be trivial, but I can't extract the quarter from a given year.

    This is my result table.
    datastr date quarter
    September 11, 1992 11sep1992 1960q4
    October 1,1992 01oct1992 1961q1
    June 15, 1994 15jun1994 1960q3
    June 20, 1994 20jun1994 1960q3
    . . .
    December 31, 2004 31dec2004 1961q1
    January 3, 2005 03jan2005 1960q2
    May 15, 2012 15may2012 1960q3
    datestr was my given variable and is formatted as string
    date I generated as
    gen date = date(datestr, "MDY")
    format date %td

    quarter I genarated as
    gen quarter= date(datestr, "MDY")
    format quarter %tq
    It didn't work. The year and the quarter were both wrong.

    I dropped it and tried with
    gen quarter= quarter(datestr)
    can't generate from strings
    gen quarter= quarter(date)
    format quarter %tq
    And I get this table. Both are wrong, again.

    Experimentally I reformatted the date variable to %tq. I not only don't get the correct values, I get values that do not match the quarter variable.

    What am I doing wrong? I tried searching through the FAQ archives, but I can't seem to find an answer. I even went to the fifth page of google, at which point I just gave up.

    Any help will be appreciated!

  • #2
    Extracting the quarter from a year is not the same as just applying a %tq format to a date--which is what you did and which produced garbage.

    What you want to do is:

    Code:
    gen quarter = qofd(date)
    format quarter %tq
    Instead of searching the archives, you need to take a break from what your doing and read the entire section on datetime variables in the [D] volume of the PDF manuals that come with your Stata installation. Select PDF Documentation from the Help menu in Stata. Then navigate to that volume and section. Reading that you will learn the overall method that Stata uses to represent dates and times, and the large array of functions that are used to create them from strings, display them in various ways, and also to convert between whole dates and parts or periods. It's a long read, and even the most experienced users have to refer back to it (or to the help files) for details. But at least you'll have a general sense of how it all works and what functions are most likely to be helpful. And you'll understand that applying a display format to a variable doesn't change its contents, and that quarters and dates are represented differently in Stata, so you can't format one into the other.

    Comment


    • #3
      Clyde gives excellent advice as always.

      A few further points:

      1. Indicating a date display format tells Stata how a numeric date is to be interpreted. Changing the format almost always produces a different interpretation. Here's today's date as I write. Numerically it is 21076, meaning days after the first day in 1960. If I format that as a quarterly date it's far into the future, 21076 quarters after the first quarter in 1960.

      Code:
      . di daily("14sep2017", "DMY")
      21076
      
      . di %td  daily("14sep2017", "DMY")
      14sep2017
      
      . di %tq  daily("14sep2017", "DMY")
      7229q1
      
      . di yq(7229, 1) - yq(1960, 1)
      21076
      Here is an analogy. You say first: A certain length is 21076 mm. Then you say, Oh no, I got the units wrong, it is 21076 km. They can't both be right. You in effect told Stata: I got the units wrong: it's not days, it's quarters.

      More at http://www.stata-journal.com/sjpdf.h...iclenum=dm0067

      Code:
      SJ-12-4 dm0067  . . . . . . . . .  Stata tip 113: Changing a variable's format
              . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
              Q4/12   SJ 12(4):761--764                                (no commands)
              discusses what changing a variable's format does and does
              not mean
      2. quarter() gives the quarter of the year, not a quarterly date.

      You don't have to Google to find out what the function does. Stata tells you directly

      Code:
      help quarter()
      takes you directly to the function help, which says

      Code:
          quarter(e_d)
             Description:  the numeric quarter of the year corresponding to date
                           e_d
             Domain e_d:   %td dates 01jan0100 to 31dec9999 (integers -679,350 to
                           2,936,549)
             Range:        integers 1 to 4 or missing
      The parentheses () spell out that you mean the function so named. This detail in turn is not esoteric: It's explained in

      Code:
      help help
      3. Recently I posted some commands as another way to handle date variables. Chiefly they bundle existing functions. See
      https://www.statalist.org/forums/for...date-variables

      Let's revisit your example -- presented as we ask you to present using dataex (SSC); see https://www.statalist.org/forums/help#stata -- and show how those commands could be used in two steps. First, we get a numeric daily date variable; then we convert it to a quarterly date variable.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str18 datastr
      "September 11, 1992"
      "October 1,1992"    
      "June 15, 1994"     
      "June 20, 1994"     
      "."                 
      "December 31, 2004" 
      "January 3, 2005"   
      "May 15, 2012"      
      end
      
      . numdate daily date=datastr, pattern(MDY)
      commas converted to periods
      (1 missing value generated)
      
      . l
      
           +--------------------------------+
           |            datastr        date |
           |--------------------------------|
        1. | September 11, 1992   11sep1992 |
        2. |     October 1,1992   01oct1992 |
        3. |      June 15, 1994   15jun1994 |
        4. |      June 20, 1994   20jun1994 |
        5. |                  .           . |
           |--------------------------------|
        6. |  December 31, 2004   31dec2004 |
        7. |    January 3, 2005   03jan2005 |
        8. |       May 15, 2012   15may2012 |
           +--------------------------------+
      
      . convdate quarterly quarter=date
      
      . l
      
           +------------------------------------------+
           |            datastr        date   quarter |
           |------------------------------------------|
        1. | September 11, 1992   11sep1992    1992q3 |
        2. |     October 1,1992   01oct1992    1992q4 |
        3. |      June 15, 1994   15jun1994    1994q2 |
        4. |      June 20, 1994   20jun1994    1994q2 |
        5. |                  .           .         . |
           |------------------------------------------|
        6. |  December 31, 2004   31dec2004    2004q4 |
        7. |    January 3, 2005   03jan2005    2005q1 |
        8. |       May 15, 2012   15may2012    2012q2 |
           +------------------------------------------+
      numdate and convdate are both part of the numdate package, which must be installed before you can use it. See the thread cited. Note that default formats were applied here. The subcommand daily of numdate and the subcommand quarterly of convdate automatically imply default formats to be used.

      Comment


      • #4
        Thank you both.
        The qofd and the convdate quarterly worked flawlessly.

        Comment

        Working...
        X