Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: MM/DD/YYYY string to stata date |
Date | Tue, 5 Jun 2012 08:44:39 +0100 |
Here is another way to do it. Consider a date like "1/1/2010" and assume that there is no problem of ambiguity, e.g. you know your dates to be DMY. First, you have a problem if the second character is "/", which you can fix: replace datevar = "0" + datevar if substr(datevar, 2, 1) == "/" So "1/1/2010" is now "01/1/2010". Second, you still have a problem if the fifth character is "/", which you can fix: replace datevar = substr(datevar,1,3) + "0" + substr(datevar, 4, .) if substr(datevar, 5, 1) == "/" OR replace datevar = subinstr(datevar, "/", "/0", 1) if substr(datevar, 5, 1) == "/" Nick On Tue, Jun 5, 2012 at 7:13 AM, daniel klein <klein.daniel.81@googlemail.com> wrote: > here is one way to go. > > // split the string variable into parts > split <mystrvar> ,g(part) p("/") > > // add leading zero > forv j = 1/2 { > replace part`j' = "0" + part`j' if length(part`j') < 2 > } > > // now put it back together > g tmp = part1 + part2 + part3 > > // transfer to date > g mydate = date(tmp, "MDY") > form mydate %td > > // clean up > drop part? tmp Lars > Thank you both. > > Nick: i dont want to make it hard for Stata. How do i go from string > mm/dd/yyyy to date? * * 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/