altabq
altabq

Reputation: 1424

Stata: Convert date, quarter to year

I have a time series dataset with quarterly observations, which I want to collapse to an annual series. For that, I need to transform my date variable first.

It looks like

. list date in 1/5

    +--------+
    |   date |
    |--------|
 1. | 1991q1 |
 2. | 1991q2 |
 3. | 1991q3 |
 4. | 1991q4 |
 5. | 1992q1 |
    +--------+

Hence, to collapse, I want date (or date2) to be 1991, 1991, 1991, 1991, 1992 etc.

Once I have that, I could use collapse or tscollapse to turn my dataset into annual data.

Upvotes: 3

Views: 9743

Answers (2)

Nick Cox
Nick Cox

Reputation: 37208

Another way is just to remember that years and quarters are just integers. A little consultation of the documentation and a little fiddling around yield

  . gen Y = 1960 + floor(Q/4)

as a conversion rule to get years from Stata quarterly dates. Formatting year as a yearly date is then permissible but superfluous.

Upvotes: 4

Maarten Buis
Maarten Buis

Reputation: 2694

// create some example data 
. clear all

. set obs 5
obs was 0, now 5

. gen date = 123 + _n

. format date %tq

// create the yearly date
. gen date2 = yofd(dofq(date))

// admire the result
. list

     +----------------+
     |   date   date2 |
     |----------------|
  1. | 1991q1    1991 |
  2. | 1991q2    1991 |
  3. | 1991q3    1991 |
  4. | 1991q4    1991 |
  5. | 1992q1    1992 |
     +----------------+

Upvotes: 7

Related Questions