Announcement

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

  • Combine 2 variables 'year', 'month' into 1 quarterly variable

    Hi all,


    I need to analyze quarterly data, but I have only variables 'year' and 'month' in my data set.

    So I need to combine these 2 variables (year) and (month) into a new variable (quarter):
    E.g. 2005 and 7 (july) should become 2005q3.


    Which commands do I need to use in Stata to become this quarterly data?

    Thanks!
    Last edited by Mat Sko; 27 Apr 2019, 09:10.

  • #2
    You need functions, not commands. See help datetime

    Code:
    gen qdate = qofd(dofm(ym(year, month)))


    is one of several ways to do it. Another is

    Code:
    gen qdate = yq(year, ceil(month/3))
    The principle there is shown by

    Code:
    . mata
    ------------------------------------------------- mata (type end to exit) ------------
    : month = (1..12)
    
    : quarter = ceil(month :/ 3)
    
    : month \ quarter
            1    2    3    4    5    6    7    8    9   10   11   12
        +-------------------------------------------------------------+
      1 |   1    2    3    4    5    6    7    8    9   10   11   12  |
      2 |   1    1    1    2    2    2    3    3    3    4    4    4  |
        +-------------------------------------------------------------+
    Either way, follow with

    Code:
    format qdate %tq

    Comment


    • #3
      That was helpful!

      Thanks.

      Comment

      Working...
      X