Announcement

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

  • Date time variable- keep based on year; month, date, only

    I have a "date and time" variable (clock variable is the right terminology?)

    Code:
    . list  OCCURED_DT in 1/1
    
         +--------------------+
         |         OCCURED_DT |
         |--------------------|
      1. | 28jun2016 21:20:00 |
         +--------------------+
    
    . des OCCURED_DT
    
                  storage   display    value
    variable name   type    format     label      variable label
    -----------------------------------------------------------------------------------------------------------------------------------------------------
    OCCURED_DT      double  %tc
    1. I would like to keep only observations for 2016. Do I have to created a year variable first and then keep 2016 or can I keep 2016 observations using this clock date variable? I tried the code below but it didn't work.
    Code:
    keep if yofd(OCCURED_DT) == 2016
    2. By the same token, can I keep only observations after a specific date (regardless of time) using this variable or I have to create a date variable first? For example, I created a date variable and then:
    Code:
    keep if OCCURED_DT_DATEONLY >= td(28may2016)
    Finally, can I use this variable to keep a specific month- or again- I have to create a month variable first.

    Thank you,
    Marvin

  • #2
    Think what the function yofd() does. It maps daily dates to yearly dates. Try it:

    Code:
    . di yofd(daily("29jun2016", "DMY"))
    2016
    But you have clock date-times, not daily dates. So, it's a two-step, clock date-time to daily date to year.

    Code:
    . di yofd(dofc(clock("29jun2016 21:00", "DMYhm")))
    2016
    You need the double function call yofd(dofc())

    All documented:
    http://www.stata.com/help.cgi?dates

    That is #1. #2 and #3 yield to similar logic:

    1. What do you have? What do you want?

    2. Sometimes you need two functions.

    3. The names really are mnemonic: yofd() gives year of daily date, etc.

    Comment


    • #3
      Thanks Nick! Dates variables are a little confusing for me. I need to read the manual more deeply.

      So to keep only 2016 observations. I run the code below and it worked:

      Code:
      keep if yofd(dofc(OCCURED_DT)) == 2016
      To keep only jan 2016 observation I run the code below, but it didn't work
      Code:
      keep if mofd(dofc(OCCURED_DT)) = 2016m1
      What would be the right command to keep only observations after May 28 2016 using the date clock-date times.

      I usually create a subset of the clock date variable such as month, year or date variable and then keep or delete whatever I need. I just was wondering if it there was a direct way to do it without creating new date variables.

      Best,
      Marvin


      Comment


      • #4
        Indeed, it shouldn't work. That displayed code is wrong on at least two counts. You should show us the error message. Stata will bail out on the first error only.

        To test for equality you need to use == not =.

        2016m1 is not a numeric value. It's a numeric date displayed in a particular way.

        We can't test your data because you aren't showing any to us. Conversely, take a tip from #2 and use display to work with examples where you know the answer.

        Here is some technique. Here's a date time in January 2016. If it's tested for being in January 2016, the expression will evaluate as 1 for true. If it's tested for being in February 2016, the expression will evaluate as 0 for false.

        Code:
        . di mofd(dofc(clock("1 Jan 2016 01:23", "DMY hm"))) == ym(2016, 1)
        1
        
        . di mofd(dofc(clock("1 Jan 2016 01:23", "DMY hm"))) == ym(2016, 2)
        0
        Last edited by Nick Cox; 29 Jun 2016, 15:19.

        Comment


        • #5
          Hi Nick,

          I am sorry to go back to this. but lets say I have a date-time variable like post 1. But I just want to keep observation after 2/1/2015. Do I have to first create a date only variable
          Code:
          gen OCCUR_DT_DATE=dofc(OCCUR_DT)
          format OCCUR_DT_DATE %td
          And then
          Code:
          keep if OCCUR_DT_DATE >= td(1feb015) 
          Or can I do this directly using the date-time variable.I just want to avoid one extra step in create a date only variable.

          Thank you,
          Marvin

          Comment

          Working...
          X