Bookmark and Share

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.


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

Re: st: Problem with datetime functions


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Problem with datetime functions
Date   Wed, 12 Jun 2013 07:21:36 +0100

This is subject to the same warning as before: the default -float-
datatype is inadequate. -long- would work in this case; I still
recommend thinking -doubles- for any date-time calculations as safest.

Nick
njcoxstata@gmail.com


On 11 June 2013 19:21, Jeph Herrin <stata@spandrel.net> wrote:
> Or if you want to store as a non-date integer
>
>  gen myday2=real(string(myday,"%tdCYND"))
>
> will do.
>
> J
>
>
>
> On 6/11/2013 10:35 AM, Nick Cox wrote:
>>
>> It's not a bug in Stata. It's yours.
>>
>> On your -generate- you need a data type other than -float- to hold
>> every digit exactly. -long- should work, but whenever dates and times
>> are mentioned, thinking -double- is safe.
>>
>> -tostring, force- shows that you knowingly run risks of damaging the data.
>>
>> But why -generate- a new variable? Use the display format  %tdCYND
>>
>> clonevar myday2 = myday
>> format myday2 %tdCYND
>>
>> is a better way to proceed if you really need a new variable.
>>
>> Nick
>> njcoxstata@gmail.com
>>
>>
>> On 11 June 2013 15:18, George Vega Yon <g.vegayon@gmail.com> wrote:
>>>
>>> Dear list members,
>>>
>>> I'm facing a problem while generating an integer from day-time
>>> variable. In short, what I (in stata %td format) have is
>>>
>>> myday
>>> 01jan2002
>>> 02jan2002
>>> 03jan2002
>>> 04jan2002
>>> ...
>>>
>>> What I want is
>>>
>>> myday2
>>> 20020101
>>> 20020102
>>> 20020103
>>> 20020104
>>> ...
>>>
>>> Which is I though I could generate by:
>>>
>>> gen myday2 = year(myday)*10000 + month(myday)*100 + day(myday)
>>>
>>> But acctually returns
>>>
>>> myday2
>>> 20020100
>>> 20020102
>>> 20020104
>>> 20020104
>>>
>>> So, it seems that stata is rounding my new variable. I don't want to
>>> say that this is a bug, but what are the alternatives?.
>>>
>>> I've solve this with:
>>>
>>> format %tdCCYYNNDD myday
>>> tostring myday, u gen(myday2) force
>>> destring myday2, replace
>>>
>>> myday2
>>> 20020101
>>> 20020102
>>> 20020103
>>> 20020104
>>>
>>> But still, stata rounding my integers doesn't sound very good. Any ideas?
>>>
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index