Announcement

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

  • ISsue with QOFD quarterly dates

    Hello,

    I have a ym variable in the format %tm. I want a quarterly var and have tried gen q=qofd(ym) but it only makes two quarters, that are wrong (1960q1 and q2, not anywhere near my date ranges). I tried sorting first, checked for missing values. Not sure what I have wrong here. I tried gen q=quarter(ym) and same issue.


    Code:
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    --------------------------------------------------------------------------------
    ym              float   %tm

  • #2
    You can't apply -qofd()- to monthly dates. Its argument must be a daily date.

    Code:
    gen wanted = qofd(dofm(ym))
    format wanted %tq

    Comment


    • #3
      The clue is in the function name qofd() which signals reduction to quarterly dates of daily dates.

      Comment


      • #4
        AH, thank you both.

        Comment


        • #5
          Strictly you don't need any specific date conversion functions at all.

          Put together Stata's convention that 0 is the first monthly date in 1960 and 0 is the first quarterly date in 1960 and 3 months being in a quarter and you can get where you want to be with floor().

          Here is a sandbox and a demonstration.

          Code:
          . clear 
          
          . set obs 12 
          Number of observations (_N) was 0, now 12.
          
          . gen mdate = -4 + _n in 1/6 
          (6 missing values generated)
          
          . replace mdate = ym(2023, 6) + _n in 7/12 
          (6 real changes made)
          
          . gen qdate = floor(mdate/3)
          
          . 
          . clonevar mdate2 = mdate
          
          . clonevar qdate2 = qdate
          
          . 
          . format qdate2 %tq 
          
          . format mdate2 %tm
          
          . 
          . l, sep(3)
          
               +----------------------------------+
               | mdate   qdate    mdate2   qdate2 |
               |----------------------------------|
            1. |    -3      -1   1959m10   1959q4 |
            2. |    -2      -1   1959m11   1959q4 |
            3. |    -1      -1   1959m12   1959q4 |
               |----------------------------------|
            4. |     0       0    1960m1   1960q1 |
            5. |     1       0    1960m2   1960q1 |
            6. |     2       0    1960m3   1960q1 |
               |----------------------------------|
            7. |   768     256    2024m1   2024q1 |
            8. |   769     256    2024m2   2024q1 |
            9. |   770     256    2024m3   2024q1 |
               |----------------------------------|
           10. |   771     257    2024m4   2024q2 |
           11. |   772     257    2024m5   2024q2 |
           12. |   773     257    2024m6   2024q2 |
               +----------------------------------+

          Comment

          Working...
          X