2

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.

4

2 回答 2

6
// 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 |
     +----------------+
于 2013-06-20T14:06:00.483 回答
4

另一种方法是记住年份和季度只是整数。对文档的一点咨询和对产量的一点摆弄

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

作为从 Stata 季度日期获取年份的转换规则。将年份格式化为年度日期是允许的,但也是多余的。

于 2013-06-20T18:47:37.257 回答