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

# st: Convert from %td formatted date to yyyymmdd 8-digit integer date

 From Richard Herron To statalist@hsphsun2.harvard.edu Subject st: Convert from %td formatted date to yyyymmdd 8-digit integer date Date Sun, 18 Sep 2011 17:49:47 -0400

```I would like to convert from ddmonyyyy (formatted as %td and really
stored as days from 1/1/1960) to yyyymmdd. For example, 1/1/1960 with
format %td is a 0 that I would like to convert to integer 19600101.

I tried using -year-, -month-, and -year- and multiplying by 1e4, 1e2,
and 1, but this fails silently on at least a few dates (see code).

What am I missing? Thanks!

* begin code ------------------------------------------------

* generate dates in 1990
clear
set obs 2000
generate yyyy = 1990
generate mm = ceil(12 * uniform())
generate dd = ceil(31 * uniform())
replace dd = ceil(30 * uniform()) if mm==9 | mm==4 | mm==6 | mm==11
replace dd = ceil(28 * uniform()) if mm==2

* generate "plain language" dates
generate ddmonyyyy = mdy(mm, dd, yyyy)
format ddmonyyyy %td

* try to generate integer dates (i.e., yyyymmdd)
generate yyyymmdd = 1e4 * year(ddmonyyyy) ///
+ 1e2 * month(ddmonyyyy) + day(ddmonyyyy)
format yyyymmdd %12.0g

* here is one bad date
list if ddmonyyyy == date("23oct1990", "DMY")

* here is another bad date
list if ddmonyyyy == date("05mar1990", "DMY")

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