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: date/time question
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
RE: st: date/time question
Date
Thu, 10 Feb 2011 13:04:03 +0000
Let's put this together. I'll go for the + ":30" solution on the argument that half-way through a minute is your best guess on when something happened if you don't know otherwise. Change to something else if that does not seem the best you can do.
clonevar mytime2 = mytime
gen ncolons = length(mydate) - length(subinstr(mydate, ":", "",.))
replace mytime2 = mytime2 + ":30" if ncolons == 1
replace mytime2 = reverse(subinstr(reverse(mytime2), ":", ".", 1)) if ncolons == 3
gen double numtime = clock(mytime2, "DMY hms")
format numtime %tC
l if missing(numtime)
Nick
[email protected]
Nick Cox
In addition if you have only one colon (no seconds), just add ":00" or
":30" (whichever makes more sense) to such values.
On Thu, Feb 10, 2011 at 10:40 AM, Nick Cox <[email protected]> wrote:
> On the last
>
> "26/12/2008 09:51:17:210"
>
> is problematic but
>
> "26/12/2008 09:51:17.210"
>
> isn't. So whenever you have three colons the last must be changed to a
> stop or period.
>
> A test for three colons is
>
> if (length(mydate) - length(subinstr(mydate, ":", "",.))) == 3
>
> and you can change it by
>
> reverse(subinstr(reverse(mydate), ":", ".", 1))
>
> Code not tested.
>
> On Thu, Feb 10, 2011 at 10:27 AM, Dimitri Szerman <[email protected]> wrote:
>
>> I have a string variable which contains information on dates and time.
>> It looks like this:
>>
>> input str23 mytime
>> "24/12/2008 10:21"
>> "25/12/2008 09:01:27"
>> "25/12/2008 09:23:23"
>> "25/12/2008 09:36:03"
>> "26/12/2008 09:00:27:803"
>> "26/12/2008 09:31:43:163"
>> "26/12/2008 09:49:03:943"
>> "26/12/2008 09:49:35:913"
>> "26/12/2008 09:51:17:210"
>> end
>>
>> I'm trying to convert this into a numeric variable. My ideas was
>>
>> gen double numtime = clock(mytime, "DMY hms")
>> format numtime %tC
>>
>> But that's giving me missing values. I guess my problem is, the
>> appropriate mask changes (for observation 1 it should be "DMY hm", for
>> observations 2-4, "DMY hms", and for observations 5-9, I have no
>> idea).
>>
>> By the way, I'm importing many ASCII files in loop, so sometimes I
>> have only the "DMY hm" format, sometimes only the "25/12/2008
>> 09:01:27" format, and sometimes a mix of the three formats above.
>>
>> Any ideas on how to solve this problem efficiently?
*
* 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/