Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Extract date and time from a date/time variable


From   "Christian Holz (Stata list)" <statalist@holz.ca>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Extract date and time from a date/time variable
Date   Wed, 11 May 2011 09:59:13 -0400

Hi,
Stata help under "whelp dates and times" reads as follows:

Let t be a %tc variable.  The following functions will extract components of t:

            ----------------------------------------------------------------
                                        Result if t = tc(05jul1972-21:38:02)
            Function         Returns         (i.e., t = 394,839,482,000)
            ----------------------------------------------------------------
            hh(t)            time of day, hours       21
            mm(t)            time of day, minutes     38
            ss(t)            time of day, seconds     2.000
            ----------------------------------------------------------------

Thus, you can extract the hours, minutes and seconds into new variables as

generate hrs=hh(ActivityDate)
generate mins=mm(ActivityDate)
generate secs=ss(ActivityDate)

I don't think the way Stata deals with date and time allows you to
store times (i.e. as sort of a "time" data type) without a date, but
you could generate (e.g. for display purposes) a string variable from
these hrs, mins, secs variables or calculate a "seconds since
midnight" variable.

Best,
Christian

On Wed, May 11, 2011 at 9:46 AM, Pinaki Mitra <mitrap@comcast.net> wrote:
> Hello,
>
>    I have a date/time variable "ActivityDate" which is double and in %tc format. For example, a value of "ActivityDate" is "01mar2011 11:56:00". I need to extract date and time into two variables. I did gen ActivityDay=dofc( ActivityDate) and format ActivityDay %dN/D/Y which provided me the date as 03/01/11. What can I do to extract "11:56:00" into a new variable "ActivityTime"?
>
> Thank you,
> Pinaki Mitra
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index