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: Convert from %td formatted date to yyyymmdd 8-digit integer date


From   Richard Herron <[email protected]>
To   [email protected]
Subject   Re: st: Convert from %td formatted date to yyyymmdd 8-digit integer date
Date   Mon, 19 Sep 2011 11:40:27 -0400

OK, in Stata I shouldn't change how dates are _stored_, just how they
are _displayed_.  This is a nice improvement over R.

I found that I can make a custom 8-digit date with -format my_date
%tCYND-. This yields the YYYYMMDD format.

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

* generate dates in 1990
clear
set obs 2000
generate long yyyy = 1990
generate long mm = ceil(12 * uniform())
generate long 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 long ddmonyyyy = mdy(mm, dd, yyyy)
format ddmonyyyy %td

* try to generate integer dates (i.e., yyyymmdd)
generate long yyyymmdd = ddmonyyyy
format yyyymmdd %tdCYND

* this fixes the "bad dates"
list if ddmonyyyy == date("23oct1990", "DMY")
list if ddmonyyyy == date("05mar1990", "DMY")

* ----- end code -----


On Sun, Sep 18, 2011 at 22:38, Richard Herron
<[email protected]> wrote:
> The problem was due to how my variables were cast. I was using -min-
> to find the earliest date, but not using -generate long- and was
> instead getting a float that was causing some trouble with my
> conversion to eight digits.
>
> This code shows how to remedy:
> * ----- begin code -----
>
> * generate dates in 1990
> clear
> set obs 2000
> generate long yyyy = 1990
> generate long mm = ceil(12 * uniform())
> generate long 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 long ddmonyyyy = mdy(mm, dd, yyyy)
> format ddmonyyyy %td
>
> * try to generate integer dates (i.e., yyyymmdd)
> generate long yyyymmdd = 1e4 * year(ddmonyyyy) ///
>    + 1e2 * month(ddmonyyyy) + day(ddmonyyyy)
> format yyyymmdd %12.0g
>
> * this fixes the "bad dates"
> list if ddmonyyyy == date("23oct1990", "DMY")
> list if ddmonyyyy == date("05mar1990", "DMY")
>
> * ----- end code -----
>
> I am still interested in a more fool proof conversion to eight digits.
>
>
> On Sun, Sep 18, 2011 at 17:49, Richard Herron
> <[email protected]> wrote:
>> 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/
>>
>

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index