Announcement

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

  • convert from unix/epoch time

    I am trying to convert from unix/epoch time to stata time.
    My timestamp data is like below.
    Type: Numeric (double)
    Range: [1.697e+12, 1.697e+12] Units: 1000

    Mean: 1.7e+12
    Std. dev.: 2.0e+07

    Percentiles: 10% 25% 50% 75% 90%
    1.7e+12 1.7e+12 1.7e+12 1.7e+12 1.7e+12
    I tried the following command, but the result remains unreadable.
    gen double statatime=timestamp*1000+mdyhms(1,1,1970,0,0,0)
    format %tc statatime


    result:
    Type: Numeric (double)
    Range: [1.697e+15,1.697e+15] Units: 100000
    I would really appreciate it if someone could tell me how to convert unix/epoch data.

    Best regards,

  • #2
    I think your code is completely correct, and I believe it is working properly.

    The problem is that you are trying to look at it in the output of the -codebook- command. -codebook- is a very old Stata command that precedes Stata's adoption of datetime variables. It does not have the capability of displaying numbers in %tc format, so what you see is the underlying double-precision floating point number, rounded to a small number of significant figures. The same limitation almost applies to the -summarize- output you show. The difference is that if you add the -format- option to your -summarize- command it will display the results in %tc format.

    If you want to visually verify that your code has worked, run -browse statatime- and there you will be able to see it properly displayed in %tc format.

    Comment


    • #3
      Do you need to multiple the Unix time by 1000?

      Code:
      . disp  1697217553000
      1.697e+12
      
      . disp  %tc (1697217553000 + mdyhms(1,1,1970,0,0,0) )
      13oct2023 17:19:13

      Comment


      • #4
        Thank you! I was able to succeed following your advice. (by dropping the multiplication of the Unix time by 1000 and not seeing codebook).

        Comment

        Working...
        X