Announcement

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

  • Handling time and date

    Dear all,
    I'm very new to Stata, I'm more used to SPSS. I need to define missing values for time variable, but no matter what I try it doesn't work, and I can't seem to find answers in any material.
    This is how I defined the variable (this part worked out well):
    gen double SDf= SEf - SOf
    format %tcHH:MM SDf

    (this did not)
    *Dealing with suspicious values
    recode SDf (tc(31dec1959 00:00:00)/tc(31dec1959 03:27:00) = tc(31dec1959 00:00:00)) (tc(31dec1959 14:00:00)/max = tc(31dec1959 00:00:00)), copyrest
    mvdecode SDf , mv(tc(31dec1959 00:00:00))

    To cut a long story short, I need to define values up to 3:27 a.m. as missing and from 2 p.m. and higher as missing. Since I defined SDf variable as "tc" I thought I could work it out the same while recoding it and then marking as missing. However, I'm just getting "unknown el tc in rule".
    Please, can anyone help me how to handle this? My life very much depends on it...

  • #2
    It is a particularly cruel thing to have your life depend on datetime conversions. I hope this is of help to you.
    Code:
    . clear
    
    . input str20 date
    
                         date
      1. "31dec1959 03:26:00"
      2. "31dec1959 03:27:00"
      3. "31dec1959 13:00:00"
      4. "31dec1959 14:00:00"
      5. end
    
    . 
    . gen double date1 = clock(date, "DMYhms")
    
    . format %tc date1
    
    . 
    . gen hour_fraction = hh(date1) + mm(date1)/60 + ss(date1)/3600
    
    . replace date1 = . if hour_fraction < 3.45 | hour_fraction >= 14
    (2 real changes made, 2 to missing)
    
    . list, abbr(14) noobs
    
      +---------------------------------------------------------+
      |               date                date1   hour_fraction |
      |---------------------------------------------------------|
      | 31dec1959 03:26:00                    .        3.433333 |
      | 31dec1959 03:27:00   31dec1959 03:27:00            3.45 |
      | 31dec1959 13:00:00   31dec1959 13:00:00              13 |
      | 31dec1959 14:00:00                    .              14 |
      +---------------------------------------------------------+

    Comment


    • #3
      That's the perk of being a PhD student tho Thank you so much! It works perfectly and my life is safe now

      Comment

      Working...
      X