Mariano wrote:
I have Stata 8, and I'm trying to generate a new variable which indicates
the elapsed time (in minutes or seconds) between two events. The variables
I want to substract are stored as str24, and contain both date and time in
the same cell
03/07/06 17:15:00, 000000
03/07/06 18:05:00, 000000
I would like to generate a numerical variable which indicates that the
elapsed time between the two events is 50 minutes. How is this possible?
===============================================================
Martin answered, but his suggestion requires Stata 10. To combine date
and time-of-day information in prior versions requires some -egenmore-
functions (unofficial -egen- functions); it includes quite a few
date-and-time functions. To get them:
ssc install egenmore
Having done that, try this:
clear
input str17 sdatetime
"03/07/06 17:15:00"
"03/07/06 18:05:00"
end
gen str8 sdate = substr(sdatetime,1,8)
gen str8 stime = substr(sdatetime,10,8)
* assuming that your date format is month, day, year:
gen date = date(sdate,"mdy",2010)
egen seconds = seconds(stime)
gen double minutes = date*24*60 + seconds/60
gen elapsed = minutes - minutes[_n-1]
list , clean
Hope this helps
Svend
________________________________________________________
Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000 Aarhus C, Denmark
Phone, work: +45 8942 6090
Phone, home: +45 8693 7796
Fax: +45 8613 1580
E-mail: [email protected]
_________________________________________________________
*
* 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/