Announcement

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

  • Extracting hour dummies from datetime variable

    Dear Statalisters,
    I am working with a panel data that includes a datime variable (Cdayhour, ex: 21feb2020 12:00:00). I would like to generate hour dummies from 1 to 24 to use in my regression analysis from this variable. First, I will generate a hour variable from 1 to 24 and then I will generate 24 hour dummies by using replace h1=1 if hour==1, replace h2=1 if hour==2 and so on. Here is a sample of my data. How can I do that? Thank you in advance.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double Cdayhour
     1.213452e+12
     6.937956e+11
    1.6094664e+12
     9.721764e+11
    1.7769492e+12
     8.787348e+11
    1.8362196e+12
     7.603272e+11
    1.8824472e+12
    1.4913972e+12
    1.1677428e+12
     9.458712e+11
      9.70236e+11
     1.518876e+12
    1.0153368e+12
     6.822216e+11
    1.0303272e+12
    1.0543032e+12
     6.869304e+11
    1.1674836e+12
    1.5834168e+12
    1.5208632e+12
    1.6498152e+12
     1.024146e+12
     9.376164e+11
    1.0314936e+12
     7.712532e+11
      8.92476e+11
     1.851516e+12
    1.2027816e+12
    1.0439784e+12
    1.0292904e+12
     9.299268e+11
     6.457572e+11
    1.3521636e+12
    1.7063604e+12
     9.489348e+11
     1.803132e+12
     1.769346e+12
      1.73025e+12
      1.31454e+12
    1.8387252e+12
     1.017666e+12
       9.3753e+11
      1.14705e+12
      9.15282e+11
      1.47114e+12
    1.3566564e+12
    1.8312948e+12
     7.491348e+11
    1.8099144e+12
     1.139274e+12
     7.649064e+11
    1.4739912e+12
     7.378596e+11
     9.210276e+11
    1.2262788e+12
    1.3088376e+12
     9.380916e+11
    1.5714072e+12
    1.8335016e+12
    1.5095016e+12
    1.4397768e+12
     8.694072e+11
    1.1337876e+12
    1.8573048e+12
    1.5084648e+12
     8.103492e+11
    1.7817444e+12
    1.9071108e+12
    1.8554904e+12
    1.0615176e+12
    1.1025144e+12
      9.37314e+11
    1.7812296e+12
     8.595108e+11
    1.6507224e+12
    1.4336856e+12
    1.9134216e+12
    1.8995076e+12
    1.8771768e+12
    1.8312552e+12
    1.8908712e+12
    1.4168376e+12
     9.315684e+11
    1.7884008e+12
    1.5847992e+12
    1.7308152e+12
    1.0510164e+12
    1.6791876e+12
      6.95916e+11
    1.1091672e+12
     1.867626e+12
    1.4479812e+12
     9.370548e+11
    1.5004296e+12
     9.101412e+11
     1.041426e+12
       8.8794e+11
    1.5845832e+12
    end
    format %tc Cdayhour

  • #2
    Code:
    gen hr = hh(Cdayhour)
    foreach x of numlist 1/24{
        gen h`x' = 0
        replace h`x' = (hr == `x')
    }

    Comment


    • #3
      generate your hour variable as follows:
      Code:
      gen hour=hh(Cdayhour)
      you can find more on this at
      Code:
       Extracting time-of-day components from datetimes
        within
      help datetime
      you say you want dummies but not why; if for inclusion in some kind of model you are almost certainly better off using factor variable notation; see
      Code:
      help fvvarlist

      Comment


      • #4
        I appreciate for your help. Thank you very much!

        Comment

        Working...
        X