Announcement

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

  • Converting date/time into a numeric format

    Dear forum,
    I have a Stata dataset which lists date/time like this: 18jul2019 08:12:04

    However, I need to be able to run models with observations from particular time periods and, therefore, need this in a numeric format. Would anyone know how to do this? I just need the date, rather than the time. I'd be extremely grateful for any help,
    Tom

  • #2
    Well, there are two possibilities here. One is that the variable you already have is, in fact, numeric ans has been given a display format that causes Stata to display it the way you show. The other possibility is that what you have is a string variable, in which case you need to create a new variable that is a proper Stata numeric variable for the date.

    You can tell which is your situation by running the -describe- command on that variable. If the output for that variable shows a storage type of str# (where # is some number or L), then you have a string variable. If it shows a storage type of int, float, or double, you have a numeric variable already.

    To convert the string variable to a numeric variable do this:
    Code:
    gen double date_time_var = clock(string_var, "DMYhms")
    assert missing(date_time_var) == missing(string_var)
    format date_time_var %tc
    gen date_var = dofc(date_time_var)
    format date_var %td
    The above code creates two new variables: one is a numeric representation of the date and time, and the second is a numeric representation of the date only. Both have display formats assigned that makes them like like date-time and date variables respectively when you list them, but the actual internal representation is numeric and suitable for numeric calculations.

    If what you have is a numeric variable already, then the only thing you need to do is extract the date from the date-time variable. The final two lines of the code given above will do that for you.

    Evidently, this post is at least twice as long as it needs to be because you did not present the crucial information about your data in your post. In the future, when asking for help with code, always show example data using the -dataex- command. While there are situations where a coding problem can be solved without the example data, those are exceptional. Example data with metadata is almost always needed, and failure to provide it leads to either excessively long and confusing posts like mine here, or to potential responders deciding not to bother. So please always show example data when asking for help with code, and always use the -dataex- command to do that to assure that the example data can be used by those who try to help you.

    If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      OK that works - thanks so much! I really appreciate it. It was already a numeric variable. Apologies for not providing the correct information - I'll do this next time!

      Comment

      Working...
      X