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]

# st: RE: RE: changing date format

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: RE: changing date format Date Sun, 5 Sep 2010 15:27:31 +0100

```Michael and Martin are clearly both correct.

Mike was specifying "DMY" whereas his dates were "MDY". If we spell out what his choice means, "2010102", for example, means month 20, etc., which Stata can make sense of; so it returns missing.

Martin's syntax can be compressed, arguably at the expense of some readability,

gen date = mdy(
real(substr(tradedate,5,2)),
real(substr(tradedate,-2,.)),
real(substr(tradedate,1,4))
)

format date %td

Nick
n.j.cox@durham.ac.uk

Martin Weiss
============

"Is there any way I can change tradedate into Stata date format without
using Excel?"

Even if you disregarded Michael`s advice and wanted to stick to your
splitting strategy, there is no need for external software:

***********
clear*

inp str8 tradedate
20010102
20010103
20010104
20010105
20010106
20010107
end

gen day=substr(tradedate,-2,.)
gen month=substr(tradedate,5,2)
gen year=substr(tradedate,1,4)
destring  day month year, replace

gen date=mdy(month,day,year)
format date %td

l
***********

Michael N. Mitchell
===================

You were so close to having it right... here is how you can do this...

clear
input str8 tradedate
20010102
20010103
20010104
20010105
20010106
20010107
end

. generate date = date(tradedate, "YMD") . format date %td . list

+----------------------+
| traded~e        date |
|----------------------|
1. | 20010102   02jan2001 |
2. | 20010103   03jan2001 |
3. | 20010104   04jan2001 |
4. | 20010105   05jan2001 |
5. | 20010106   06jan2001 |
|----------------------|
6. | 20010107   07jan2001 |
+----------------------+

Mike Kim
========

I imported daily stock data from CRSP. The data shows date like this:

tradedate
20010102
20010103
20010104
20010105
20010106
20010107
......

Neither of the following commands works:
- format tradedate %td
- gen date=date(tradedate, "MDY")

I split tradedae into month, day, year in excel, imported into Stata, and
used the following command to combine them.
- gen date=mdy(month,day,year)

Is there any way I can change tradedate into Stata date format without using
Excel?

*
*   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/
```

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index