Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: converting string to date


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: converting string to date
Date   Fri, 29 Nov 2013 18:10:09 +0000

I've got to suggest, however, that Stata's own idea of weeks, although
logical in its own way, rarely if ever corresponds to any other idea
of weeks. Thus -week()- is likely to make your problems worse.

This has often been misunderstood, even to the point that it was
suggested on this list that Stata's implementation of weeks is broken.
I'll suppress the reference....

The point is discussed in detail  in my pieces in the Stata Journal,
but a brief summary here may help.

Stata's rules are that

1. Week 1 of a year always starts on 1 January, regardless.

2. Other weeks therefore start 7, 14, 21, ... days later.

3. The last week of the year always has 8 or 9 days, depending on
whether it is not or it is a leap year. So, there are always 52 weeks
in a year, no more, no less.

Thus weeks always nest within years.

If this corresponds to what you want, that's fine. Every other system
I've seen has the idea either that weeks begin on a certain day of the
week, or that they end ditto (or clearly, both). The difference lies
in what is emphasised, not otherwise. It follows that weeks can span
two years at the end of one year and the beginning of the next.



Nick
[email protected]


On 29 November 2013 17:51, Nick Cox <[email protected]> wrote:
> My mistake: I missed the bit about weeks. Joe Canner has very good
> advice in his reply.
>
> Weeks in my view are best handled by treating them as daily dates 7 days apart.
>
> See also, if you have a serious interest in weeks,
>
> SJ-12-4 dm0065_1  . . . . . Stata tip 111: More on working with weeks, erratum
>         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>         Q4/12   SJ 12(4):765                                     (no commands)
>         lists previously omitted key reference
>
> SJ-12-3 dm0065  . . . . . . . . . .  Stata tip 111: More on  working with weeks
>         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>         Q3/12   SJ 12(3):565--569                                (no commands)
>         discusses how to convert data presented in yearly and weekly
>         form to daily dates and how to aggregate such data to months
>         or longer intervals
>
> SJ-10-4 dm0052  . . . . . . . . . . . . . . . . Stata tip 68: Week assumptions
>         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>         Q4/10   SJ 10(4):682--685                                (no commands)
>         tip on Stata's solution for weeks and on how to set up
>         your own alternatives given different definitions of the
>         week
>
> I'm still proud of the weak "week" pun behind "week assumptions". .
>
>
>
> Nick
> [email protected]
>
>
> On 29 November 2013 17:47, Nick Cox <[email protected]> wrote:
>> This works fine:
>>
>> . di %td date("2004-01-04", "YMD")
>> 04jan2004
>>
>> Note the very simple -- but  from many posts on this list seemingly
>> little known -- technique of trying out a simple example using the
>> -display- command and a format that checks whether I get the right
>> answer.
>>
>> But your approach also works fine in the examples I tried
>>
>> . di %td date("2004-01-04", "20YMD")
>> 04jan2004
>> . di %td date("1998-01-04", "20YMD")
>> 04jan1998
>>
>> so why did you say "does not seem to work"? Did you forget to assign a
>> date format so that what you see is human-readable?
>>
>>
>> Nick
>> [email protected]
>>
>>
>> On 29 November 2013 17:15, Nilay Kumar <[email protected]> wrote:
>>> I have a dataset where weeks are listed as strings ie. 2004-01-04 - 2004-01-10. How can I convert this entire string to dates?
>>> I tried gen date1= date(var, “20YMD”) but that does not seem to work for this

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index