Bookmark and Share

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

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

Re: st: Problem with datetime functions

From   Nick Cox <>
To   "" <>
Subject   Re: st: Problem with datetime functions
Date   Tue, 11 Jun 2013 20:54:52 +0100

This seems confused on several levels.

The help for -dates and times- has explicit advice about data types for dates. It's a pity if people don't read it, but there you go.

The display format is no more than that, a display format, and immaterial to merging of any kind. For linking with other software the most important predictable problem is the date origin, i.e. the calendar date which is integer zero.

Also, Stata has no date types as such: in practice, numeric variables may be assigned date formats, but that's it.

On 11 Jun 2013, at 19:43, George Vega Yon <> wrote:

Dear Nick,

Thank you for your answer. Good tip on using double when working with
dates and times, though, at least to me, it does make sense that stata
warns you (or something) in this type of operations. For most of the
people who I know that uses stata, data type distinction (float, long
or double) isn't straightforward.

I need to generate a new variable because, I merge this dataset with
another one which has a "date" variable as integer (and not as stata
date-time "data type"). Even when I prefer working with calendar
variables, almost all of the data sets in my work work with YYYYMMDD
integer format.

Best regards!,

George Vega Yon
7 647 2552

2013/6/11 Nick Cox <>:

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.


On 11 June 2013 15:18, George Vega Yon <> 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


What I want is


Which is I though I could generate by:

gen myday2 = year(myday)*10000 + month(myday)*100 + day(myday)

But acctually returns


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


But still, stata rounding my integers doesn't sound very good. Any ideas?


George Vega

PS: Running on windows 7
c(stata_version) = 12.1
c(version) = 12.1
c(born_date) = "18 Dec 2012"
c(flavor) = "IC"
c(os) = "Windows"
c(osdtl) = "64-bit"
c(machine_type) = "PC (64-bit x86-64)"
c(byteorder) = "lohi"

*   For searches and help try:

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