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: Change string to year and month date


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Change string to year and month date
Date   Sun, 20 Oct 2013 17:11:54 +0100

This can be shortened:

     gen DATE_OF_BIRTH2 = /// ym(substr(DATE_OF_BIRTH2,1,4),
substr(DATE_OF_BIRTH2,5,2))
     format DATE_OF_BIRTH2 %tmMCY

Nick
njcoxstata@gmail.com

On 20 October 2013 15:48, Li Chuntao (Tony) <leechtcn@gmail.com> wrote:
> Matthew,
>
>     I suggest you simply create two additional variables as following:
>     gen int year = real(substr(DATE_OF_BIRTH2,1,4))
>      gen int month = real(substr(DATE_OF_BIRTH2,5,2))
>
>      gen DATE_OF_BIRTH2 = ym(year,month)
>      format DATE_OF_BIRTH2 %tmMCY

On Fri, Oct 18, 2013 at 11:16 PM, DeMichele, Matthew <mdemichele@rti.org> wrote:
>> I'm trying to create a date variable using only the year and the month
>> from a string variable formatted as YYYYMM. For example, 200405 = May,
>> 2004. I ran the syntax below, but this results in DAY appearing in the
>> first two columns as "01" such that the example above would be
>> 01May2004.
>>
>> gen DATE_OF_BIRTH3= date(DATE_OF_BIRTH2, "YM")
>> format DATE_OF_BIRTH3 %td
*
*   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