Announcement

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

  • Timestamp manipulation with time variables

    I have a dataset with double timestamp and time variables like (date_only, hour, minute, and second). I tried to create a timestamp while the alert ==1, but the output is not similar to the double timestamp. ( the alert_time must have to match with a double timestamp ). Please help me with this query.
    .

    Here is the code I used to create an alert time.

    gen alert_time = dhms(date_only, hour, minute, second) if drowsy_alert ==1
    format alert_time %tc

    .
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double timestamp float(date_only hour minute second) byte alert float alert_time
    1.9770003e+12 22881 22 45  0 0             .
    1977000301000 22881 22 45  1 0             .
    1977000302000 22881 22 45  2 0             .
    1977000303000 22881 22 45  3 0             .
    1977000304000 22881 22 45  4 0             .
    1977000305000 22881 22 45  5 0             .
    1977000306000 22881 22 45  6 0             .
    1977000307000 22881 22 45  7 0             .
    1977000308000 22881 22 45  8 0             .
    1977000309000 22881 22 45  9 0             .
    1977000310000 22881 22 45 10 0             .
    1977000311000 22881 22 45 11 0             .
    1977000312000 22881 22 45 12 0             .
    1977000313000 22881 22 45 13 1 1.9770003e+12
    1977000314000 22881 22 45 14 1 1.9770003e+12
    1977000315000 22881 22 45 15 1 1.9770003e+12
    1977000316000 22881 22 45 16 1 1.9770003e+12
    1977000317000 22881 22 45 17 1 1.9770003e+12
    1977000318000 22881 22 45 18 1 1.9770003e+12
    1977000319000 22881 22 45 19 1 1.9770003e+12
    1977000320000 22881 22 45 20 1 1.9770003e+12
    1977000321000 22881 22 45 21 0             .
    1977000322000 22881 22 45 22 0             .
    1977000323000 22881 22 45 23 1 1.9770003e+12
    1977000324000 22881 22 45 24 1 1.9770003e+12
    1977000325000 22881 22 45 25 0             .
    1977000326000 22881 22 45 26 0             .
    1977000327000 22881 22 45 27 0             .
    1977000328000 22881 22 45 28 0             .
    1977000329000 22881 22 45 29 0             .
    1977000330000 22881 22 45 30 0             .
    1977000331000 22881 22 45 31 0             .
    1977000332000 22881 22 45 32 0             .
    1977000333000 22881 22 45 33 0             .
    1977000334000 22881 22 45 34 0             .
    1977000335000 22881 22 45 35 0             .
    1977000336000 22881 22 45 36 0             .
    1977000337000 22881 22 45 37 0             .
    1977000338000 22881 22 45 38 0             .
    1977000339000 22881 22 45 39 0             .
    1977000340000 22881 22 45 40 0             .
    1977000341000 22881 22 45 41 0             .
    1977000342000 22881 22 45 42 0             .
    1977000343000 22881 22 45 43 0             .
    1977000344000 22881 22 45 44 0             .
    1977000345000 22881 22 45 45 0             .
    1977000346000 22881 22 45 46 0             .
    1977000347000 22881 22 45 47 0             .
    1977000348000 22881 22 45 48 0             .
    1977000349000 22881 22 45 49 0             .
    1977000350000 22881 22 45 50 0             .
    1977000351000 22881 22 45 51 0             .
    1977000352000 22881 22 45 52 0             .
    1977000353000 22881 22 45 53 0             .
    1977000354000 22881 22 45 54 0             .
    1977000355000 22881 22 45 55 0             .
    1977000356000 22881 22 45 56 0             .
    1977000357000 22881 22 45 57 0             .
    1977000358000 22881 22 45 58 0             .
    1977000359000 22881 22 45 59 0             .
    1977000360000 22881 22 46  0 0             .
    1977000361000 22881 22 46  1 0             .
    1977000362000 22881 22 46  2 0             .
    1977000363000 22881 22 46  3 0             .
    1977000364000 22881 22 46  4 0             .
    1977000365000 22881 22 46  5 0             .
    1977000366000 22881 22 46  6 0             .
    1977000367000 22881 22 46  7 0             .
    1977000368000 22881 22 46  8 0             .
    1977000369000 22881 22 46  9 0             .
    1977000370000 22881 22 46 10 0             .
    1977000371000 22881 22 46 11 0             .
    1977000372000 22881 22 46 12 0             .
    1977000373000 22881 22 46 13 0             .
    1977000374000 22881 22 46 14 0             .
    1977000375000 22881 22 46 15 0             .
    1977000376000 22881 22 46 16 0             .
    1977000377000 22881 22 46 17 0             .
    1977000378000 22881 22 46 18 0             .
    1977000379000 22881 22 46 19 0             .
    1977000380000 22881 22 46 20 0             .
    1977000381000 22881 22 46 21 0             .
    1977000382000 22881 22 46 22 0             .
    1977000383000 22881 22 46 23 0             .
    1977000384000 22881 22 46 24 0             .
    1977000385000 22881 22 46 25 0             .
    1977000386000 22881 22 46 26 0             .
    1977000387000 22881 22 46 27 0             .
    1977000388000 22881 22 46 28 0             .
    1977000389000 22881 22 46 29 0             .
    1977000390000 22881 22 46 30 0             .
    1977000391000 22881 22 46 31 0             .
    1977000392000 22881 22 46 32 0             .
    1977000393000 22881 22 46 33 0             .
    1977000394000 22881 22 46 34 0             .
    1977000395000 22881 22 46 35 0             .
    1977000396000 22881 22 46 36 0             .
    1977000397000 22881 22 46 37 0             .
    1977000398000 22881 22 46 38 0             .
    1977000399000 22881 22 46 39 0             .
    end
    format %tc timestamp
    format %td date_only
    format %tc alert_time

  • #2
    Datetimes, whatever their origins, should always be double. This is not optional but stressed several times in users' best interests in help datetime.

    The fact that one time here was originally just to the nearest second is not material to a comparison with a time in milliseconds, as the factor 1000 is not a power of 2. You need to drop your alert_time and re-create it as a double.

    There is an alternative, which is to step outside Stata's framework and create your own machinery for handling datetimes in seconds. That alternative is feasible but not recommended.
    Last edited by Nick Cox; 10 Nov 2023, 07:25.

    Comment


    • #3
      Thanks, Nick. I found the error in my code. Your reply gave me some idea on this query

      Comment

      Working...
      X