Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: On the analysis of intraday data


From   [email protected] (William Gould, Stata)
To   [email protected]
Subject   Re: st: On the analysis of intraday data
Date   Mon, 01 Mar 2004 08:19:12 -0600

Levy Lee <[email protected]> writes, 

> [...] I don't know if stata has some builtin functions to handle time
> variables, as it does on date variables. I have a variables looks like the
> following:
>
>
>    Obs. No.                            Time
>       1                          2003-03-03 09:30:29
>       2                          2003-03-03 09:30:47
>       3                          2003-03-03 09:31:11
>        
> [...]  I wish there is some buildin function so that we can calculate
> 2003-03-03 09:30:29 minus 2003-03-03 09:30:47 will equals to 28 seconds.

There is no built-in function, but obtaining the desired result should not 
be too difficult.  Although this is "intraday" data, let's write our code 
so that, were times interday, we would still get the right answer.

            ----+----1----+----2----+----3----+----4
            2003-03-03 09:30:29

        . gen str date = substr(time, 1, 10) 
        . assert substr(time,11,1)==" "
        . gen hour = real(substr(time,12,2))
        . assert substr(time,14,1)==":"
        . gen min  = real(substr(time,15,2))
        . assert substr(time,17,1)==":"
	. gen sec  = real(substr(time,18,2))

Now that I've got the components -- and I have established that all
observations have the expected format, we can make the calculation:

        . gen edate = date(date, "ymd") 
        . gen double secs = date*24*60*60 + hour*60*60 + min*60 + sec

Variable -secs- now contains the number of seconds since 01jan1960 00:00:00.
Note that I have stored -secs- as a double.  That is important.

-- Bill
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index