Question

I currently have economic data in the format YYYY.QX where Q indicates "Quarter" followed by X, which is in [1,4]. This is interpreted as a string.

I've tried to use the date(series, "YMD") and formatting command, as well as the encode function.

Ideally, I'd end up with a numerical variable indicating something like:

  • YYYY.X
  • YYYY.M, where "M" is the first month of that quarter
  • YYYYMM01, where "MM" is the first month of that quarter.
Was it helpful?

Solution

It's best to show exactly what code you tried and what Stata did or said in response.

Such dates are quarterly dates so treating them as anything else is at best indirect and at worst quite wrong.

. set obs 1
obs was 0, now 1

. gen example = "2013.Q4"

. gen qdate = yq(real(substr(example, 1,4)),real(substr(example, -1,1)))

. list

    +-----------------+
    | example   qdate |
    |-----------------|
 1. | 2013.Q4     215 |
    +-----------------+

. format qdate %tq

 . list

    +------------------+
    | example    qdate |
    |------------------|
 1. | 2013.Q4   2013q4 |
    +------------------+

Note that your code indicating the date is a daily date can only be wrong. Also that encode (incidentally not a function, but a command) cannot help here unless you specify every string date explicitly as a value label.

UPDATE Note that the function date() is not an all-purpose function for creating any kind of date: it is only for daily dates. There is in fact a synonym daily().

This example shows that using quarterly() is another possibility.

. di quarterly(substr("2013.Q4", 1,4) + " " + substr("2013.Q4", -1,1), "Yq")
215

For a variable series containing such string dates, you could go

. gen qdate = quarterly(substr(series, 1, 4)) + " " + substr(series, -1, 1), "Yq") 
. format qdate %tq 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top