Stata, string variable to quartely time series

Viewed 25

I have a data set with this type of observations:

"2015_1"
"2015_2"
"2015_3"

I want to convert to time series(quarterly), like:

2015q1
2015q2
2015q3
1 Answers

This is a standard conversion task. See help datetime and help datetime display formats for the detail.

* Example generated by -dataex-. To install: ssc install dataex
clear
input str6 have
"2015_1"
"2015_2"
"2015_3"
end

gen wanted = quarterly(have, "YQ")
format wanted %tq

list

     +-----------------+
     |   have   wanted |
     |-----------------|
  1. | 2015_1   2015q1 |
  2. | 2015_2   2015q2 |
  3. | 2015_3   2015q3 |
     +-----------------+


describe

Contains data
  obs:             3                          
 vars:             2                          
---------------------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
---------------------------------------------------------------------------------------------
have             str6   %6s                  
wanted          float   %tq                   
-------------------------------------------------------------------------------------

Related