Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • String date time conversion to numerical date and time variable

    Hi all,

    I'm using Stata v13.1 in Windows and on a network installation. I have a variable (x526_) containing a string date time value such as

    05/01/2014 10:15

    I want to split this variable and then generate two extra numeric variables containing 'date' and 'time'.

    I can do this using the following code (I loop through a number of variables):

    split x526_

    foreach var of varlist x526_ {
    g `var'_date = date(`var', "DMYhm")
    replace `var'_date = date(`var', "DMY") if `var'_date==. & `var'!=""
    format `var'_date %td
    g `var'_time = clock(`var', "DMYhm")
    format `var'_time %tc_HH:MM
    }


    However, I observe that when generating the time(clock) aspect of the date time variable, Stata is not converting the string date time in an exact form - i.e. it is either adding or subtracting a minute from the old time variable when creating the new time variable - this does not appear to be consistenly applied either. This could have important consequeneces to my analysis. Anyone have a clue what is going on here?


    Here are some examples from the editor window:
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	6.3 KB
ID:	1295607


    If I look at the final column shown below (z526__time), the full contents of the entry for the penultimate record is 16jan2012 07:44:12 - and I'm surprised as I don't actually supply this infoirmation from the original string value.

    Thanks

    Tim
    Last edited by Tim Evans; 26 May 2015, 05:14.

  • #2
    You've broken a cardinal rule: date-times must always be generated as double. What looks like a capriciously appearing bias is just imprecision.

    By the way, your loop is a loop over precisely one variable. (Perhaps it's a simplification of original code complicated in a way that you simplified out.)
    Last edited by Nick Cox; 26 May 2015, 07:20.

    Comment


    • #3
      Nick, Thanks for this. I will go back and generate my date-times as doubles and confirm that resolves my problem. For simplicity, I removed the other variables that are part of the loop.

      Comment


      • #4
        One of my top tips with date-times, ridiculously simple except that people won't try it!, is to use display for examples where you know the answer.

        Code:
        . di %tc clock("16 Jan 2012 07:45", "DMY hm")
        16jan2012 07:45:00
        clock() will echo your date-time as you have it, so the error lies elsewhere, and it's in the variable type chosen by default.

        Comment

        Working...
        X