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: Date format from string


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Date format from string
Date   Tue, 11 Mar 2014 10:19:50 +0000

The error is "type mismatch". That means that Stata expects numeric
but sees string, or vice versa. In this case Stata expects string.

So, the details of what's inside the date are as yet quite secondary.

Compare the following examples:

. di date(20012011, "DMY")
type mismatch
r(109);

. di date("20012011", "DMY")
18647

. di date(string(20012011), "DMY")
.

. di date(string(20012011, "%12.0f"), "DMY")
18647

In your case, try

 gen eventdate = date(string(mydatevariable, "%12.0f"), "DMY")

The third example above shows the default format of -string()- does
not help here. Why is that?

. di string(20012011)
2.00e+07

-date()- can't make sense of that as a daily date.

The word "format" is slippery here. None of your "formats" is in fact
a Stata format, although your meanings were all clear.

Nick
[email protected]


On 11 March 2014 10:07, Philipp Hagspiel <[email protected]> wrote:

> I have imported and reshaped an excel file with dates as variables
> (observations after reshaping). They are now in the string format ddmmyyyy.
> They used to be in the format dd.mm.yyy in excel but I put a stub in front
> of every date, so I guess that is why it didn't keep the date format.
>
> When I know want to use the date function now, it tells me that there is a
> mismatch.
>
> . gen eventdate = date(mydatevariable, "DMY")
>     type mismatch
>     r(109);
>
> I have noticed that all dates that start with a "0" on the dd are converted
> into a d format.
> So: 01.01.2001 --> 1012011, 20.01.2011 --> 20012011
>
> Thus, I know have two formats: ddmmyyyy and dmmyyyy (whenever the day is
> between 01-09 it is now 1-9).
>
> How can I fix 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