Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: string date

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: string date Date Thu, 2 Feb 2012 12:26:45 +0000

```Sorry, correction: That is an alternative to yofd(dofq(yourvar)).

Nick

On Thu, Feb 2, 2012 at 9:50 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> An alternative to quarter(dofq(yourvar)) is 1959 + ceil(yourvar/4).
> This follows directly from the rule that quarters run 1 ... 4 in each
> calendar year and Stata's rule that the first date of any kind in 1960
> is numbered 1. So in 1960 quarterly dates run 1..4 which round up to 1
> when divided by 4 and in 2012 quarterly dates run 208 ... 212 which
> round up to 53 when divided by 4.
>
> On Wed, Feb 1, 2012 at 11:39 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>> There is some ambiguity here.
>>
>> If -yourvar- is a numeric variable with such a format assigned then
>> the year is yofd(dofq(yourvar)) and it is itself a quarterly date. The
>> quarter of year which runs 1...4 is quarter(dofq(yourvar)) or
>> mod(yourvar, 4) + 1.
>>
>> If -yourvar- is string, then year is real(substr(yourvar,1,4))  and
>> the quarter of the year is real(substr(yourvar, -1, 1)).
>>
>> To solve these problems, you just have to read the help to find the
>> right functions. Nothing is esoteric or hidden; equally there are so
>> many possibilities that it can easily take a few minutes to find the
>> solution.
>
> On Wed, Feb 1, 2012 at 11:20 PM, Rui Zeng <rzeng@wisc.edu> wrote:
>
>>> How can I extract year and quarter from a string format date suhc as
>>> "1987q1"? I can not use the format command to transfer from string to %tq
>>> format. thanks!

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```