I have a dataset like this
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str19 time_org float id
"2009-01-03 18:15:05" 0
"2009-01-09 02:54:25" 1
"2009-01-09 02:55:44" 2
"2009-01-09 03:02:53" 3
"2009-01-09 03:16:28" 4
"2009-01-09 03:23:48" 5
"2009-01-09 03:29:49" 6
"2009-01-09 03:39:29" 7
"2009-01-09 03:45:43" 8
"2009-01-09 03:54:39" 9
end
The original time_org variable is a string one, so I converted it then generate a new variable, time
Code:
gen year = substr(time_org,1,4)
gen month = substr(time_org,6,2)
gen day = substr(time_org,9,2)

gen hour = substr(time_org,12,2)
gen min = substr(time_org,15,2)
gen second = substr(time_org,18,2)

foreach x of varlist year month day hour min second {
    destring `x', replace
    }
*
gen time = mdyhms(month,day,year,hour,min,second)
Next, I create a lag1 variable and make a calculate on time difference between time and lag1.
Code:
gen lag1 = time[_n-1]
gen period = (time-lag1)/1000
list in 1/10
I have 2 questions and need your help:

1) It seems the results for time difference in seconds are incorrect.
2) If I set the datetime format with the code below, the time_org and time are different. How to fix it?
Code:
format time %tc
I tried with code below and results are incorrect too.
Code:
gen time = Cmdyhms(month,day,year,hour,min,second)
format time %tC
Thank you so much.