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: add hours to date variable


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: add hours to date variable
Date   Sat, 28 Aug 2010 19:26:33 +0000

<>

You want to use the -dhms()- function.  
Consult -help dates_and_times- for more.

**************!
**set-up**
clear
inp str15(date hour)
"04/29/2001" "3.75"
"05/01/2002"  "4.25"
"06/01/2004" "23.56"
"07/01/2005"  "1.00"
end
****
//note: working with date/time as strings//
			//yours appear to be numeric//
cap tostring date, replace u  
cap tostring hour, replace u
****
//hours & minutes//
split hour, p(".")
rename hour2 parthour
destring hour1 , replace
destring parthour, replace
g minutes = 60*(parthour/100)
drop parthour

//minutes & seconds//
cap tostring minutes, replace force u
split minutes, p(".")
rename minutes2 seconds
drop minutes
destring minutes1, force replace 
destring seconds, replace 
replace seconds = cond(mi(seconds), 0, 60*(seconds/10))

**create date variable**
g double finaldate = dhms(date(date, "MDY"), hour1, minutes1, seconds)
format finaldate %tc
li date hour finaldate
**************!

~ Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754


On Aug 28, 2010, at 1:40 PM, Caleb Southworth wrote:

> How can I add hours to a date variable? The hours are in decimal, such that 13:45 = 13.75, etc.
> 
> . sum date hour
> 
>    Variable |       Obs        Mean    Std. Dev.       Min        Max
> -------------+--------------------------------------------------------
>        date |       735       18428    70.77298      18306      18550
>        hour |       735    12.99728     4.90426          6         19
> 
> . desc date hour
> 
>              storage  display     value
> variable name   type   format      label      variable label
> -------------------------------------------------------------------------------------------------------------
> date            float  %td
> hour            byte   %9.0g
> 
> 
> Caleb Southworth
> Associate Professor
> Univ. of Oregon Sociology
> *
> *   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index