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 string variable to date


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: convert string variable to date
Date   Mon, 21 Jan 2013 11:37:50 +0000

So, you need to remove "st" "rd" "th" from that variable, or to be
safer a copy of that variable.

clonevar admissiondate2 = admissiondate

foreach s in st rd th {
         replace admissiondate2 = subinstr(admissiondate2, "`s'", "", .)
}

Then use -date()-. If there are still missings you will need to look
at the dates that are still problematic:

l admissiondate2 if missing(date(admissiondate2, "DMY"))

For "STATA" read "Stata".

Nick

On Mon, Jan 21, 2013 at 11:18 AM, Neophytos Stylianou
<[email protected]> wrote:
> Hello,
>
> I have a problem with 2 of my variables which I want to convert form string to dates. One of the variable is "admissiondate" seen below:
>
>                                 ADMISSIONDATE
> type:   string (str13)
>
> unique values:  3287    missing "":     0/59047
>
> examples:       "16th Aug 2007"
>         "21st Feb 2007"
>         "27th Jan 2011"
>         "4th Apr 2007"
>
> warning:        variable has embedded   blanks
>
>
> My other variable is "dichordeathdate":
>
>                                         DISCHORDEATHDATE
> type:   string (str25)
>
> unique values:  3335    missing "":     0/59047
>
> examples:       "16th Apr 2011"
>         "21st Aug 2011"
>         "27th Feb 2004"
>         "3rd Sep 2009"
>
> warning:        variable has embedded   blanks
>
> How can I convert them to dates?
>
> I do not think the command """gen admdate2 = date(admissiondate, "DMY")""" works here since I do have the 4digit year but I have the rd,st,th after the day!
>
> Is there an easy way to covert the variable to date variable without having to rewrite the whole dates?
>
> I am working on STATA 12
*
*   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