Announcement

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

  • Extracting quarter from year quarter date

    Dear All,

    I got stuck with my work. I have year-quarterly variable t e.g. 2020q1. I wanted to extract only quarter from it. I used the command:
    Code:
    gen quarter=qofd(yofd(t))
    Stata shows me that ​​​everything is 21 quarter...
    I don't understand the logic behind this. I wanted to extract year and used the code:
    Code:
    gen year=yofd(dofq(t))
    and got the result.

  • #2
    quarter() feeds on daily dates, so you need to map quarterly dates to daily dates first.


    Code:
    . di yq(2022, 2)
    249
    
    . di %tq  yq(2022, 2)
    2022q2
    
    . di quarter(dofq(yq(2022, 2)))
    2
    Don't shoot the messenger. Alternatively extrdate from numdate from SSC does it all for you.


    Code:
    . gen have = yq(2022, 2)
    
    . format have %tq
    
    
    . extrdate quarter  quarter = have
    
    
    . l have quarter in 1
    
         +------------------+
         |   have   quarter |
         |------------------|
      1. | 2022q2         2 |
         +------------------+

    Comment


    • #3
      You can exploit the fact that the quarter is the last digit when displaying a quarterly date variable.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float quarter
      198
      244
      245
      end
      format %tq quarter
      
      gen wanted= real(substr(string(quarter,"%tq"), -1, 1))
      Res.:

      Code:
      . gen wanted= real(substr(string(quarter,"%tq"), -1, 1))
      
      . l
      
           +------------------+
           | quarter   wanted |
           |------------------|
        1. |  2009q3        3 |
        2. |  2021q1        1 |
        3. |  2021q2        2 |
           +------------------+
      Note: Crossed with #2.

      Comment


      • #4
        Alternatively the quarter is 1 + mod(quarterly_date, 4).

        Comment


        • #5
          Thank you Nick and Andrew for your answers. When I typed gen quarter=quarter(dofq(yq(time))) I saw invalid syntax.
          Unfortunately I cannot install numdate. When I typed ssc install numdate I saw: host not found.
          I tried to understand how the command in the #4 works. I found that mod(x,y) produces remainders of x from division by y.
          Since a year has 4 quarters I understand we have to divide time by 4. I don't understand why we are adding 1.

          Anyway, I got my result and I am very grateful for your kind help!

          Comment


          • #6
            yq() requires two arguments -- the year and the quarter -- and you supplied one.

            If you divide integers by 4 then there are 4 possible remainders, namely 0 1 2 3. That is why you need to add 1 because you want to map to quarters 1 2 3 4.

            Here is that done more slowly. I created a sandbox dataset in which we know the quarter. But the point is how do we extract the quarter from a quarterly date?

            Code:
            . clear
            
            . set obs 8
            Number of observations (_N) was 0, now 8.
            
            . gen year = cond(_n <= 4, 2020, 2021)
            
            . egen quarter = seq(), to(4)
            
            . gen qdate = yq(year, quarter)
            
            . format qdate %tq
            
            . list, sepby(year)
            
                 +-------------------------+
                 | year   quarter    qdate |
                 |-------------------------|
              1. | 2020         1   2020q1 |
              2. | 2020         2   2020q2 |
              3. | 2020         3   2020q3 |
              4. | 2020         4   2020q4 |
                 |-------------------------|
              5. | 2021         1   2021q1 |
              6. | 2021         2   2021q2 |
              7. | 2021         3   2021q3 |
              8. | 2021         4   2021q4 |
                 +-------------------------+
            
            . gen remainder = mod(qdate, 4)
            
            . gen wanted = remainder + 1
            
            . list, sepby(year)
            
                 +---------------------------------------------+
                 | year   quarter    qdate   remain~r   wanted |
                 |---------------------------------------------|
              1. | 2020         1   2020q1          0        1 |
              2. | 2020         2   2020q2          1        2 |
              3. | 2020         3   2020q3          2        3 |
              4. | 2020         4   2020q4          3        4 |
                 |---------------------------------------------|
              5. | 2021         1   2021q1          0        1 |
              6. | 2021         2   2021q2          1        2 |
              7. | 2021         3   2021q3          2        3 |
              8. | 2021         4   2021q4          3        4 |
                 +---------------------------------------------+

            Comment


            • #7
              Thank you for such a thorough explanation! Now I got it

              Comment

              Working...
              X