Statalist


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

RE: st: date-time help


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: date-time help
Date   Sun, 18 Jan 2009 17:00:52 -0000

In addition, -ntimeofday- and -stimeofday- from the Stata Journal files
are written for Stata 8. 

Nick 
[email protected] 

Svend Juul

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
 

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index