Announcement

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

  • Help w/ dates!

    I'm working with a dataset with dates in this format: 2021-11-29T23:40:30.000Z

    the variable is called created_at

    I need to drop all observations after this point, but I can't figure out how. Do I need to change the format first? If so, to what and how? I think I know how to drop after that but I can't figure out how to reconfigure the dates. I'm very new at this and any help would be appreciated!

  • #2
    You need to convert those strings to proper Stata internal format numerical clock variables. It looks like this:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str24 var1
    "2021-11-29T23:40:30.000Z"
    end
    
    gen double timestamp = clock(var1, "YMD#hms#")
    assert missing(timestamp) == missing(var1)
    format timestamp %tc
    Then if the particular time threshold after which observations should be dropped is the example you show, you can do
    Code:
    drop if timestamp > tc(29nov2021 23:40:30.000)
    In the future, when showing data examples, please use the -dataex- command to do so, as I have done here. If you are running version 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
      Welcome to Statalist. Please take a moment to read the FAQ at http://www.statalist.org/forums/help. In Section 12 it describes how to provide necessary information so that users can understand the data and help. Particularly the use of dataex to show sample data. It will allow us to know how created_at is formatted; currently it can be a Stata-recognized date, or it can be a character-based variable, and these options lead to different recommendations.

      Comment


      • #4
        Because the date-times are formatted in conformance with the international standard ISO 8601, you can just leave them as strings.

        That is,
        Code:
        drop if created_at > "2021-11-29T23:40:30.000Z"
        will do it.

        Comment

        Working...
        X