舍入日期时间变量

时间:2018-10-30 01:44:10

标签: stata

我在Stata中具有以下日期时间数据:

clear
input double t
 1261445301998.997
1261445361999.0017
     1261445421999
1261445481999.0002
 1261445541998.999
end

如何将它们四舍五入到最近的小时?

1 个答案:

答案 0 :(得分:2)

Stata的内部格式(SIF)for datetime以毫秒为单位。

给出

1 h = 60 min
1 min = 60 s
1 s = 1000 ms

然后

1 h = 60 min x 60 s/min x 1000 ms/s = 3600000 ms

因此,您可以将t舍入为该值:

generate double t2 = round(t, 3600000)
format %tcDD-NN-CCYY_HH:MM:SS t t2

list

     +-------------------------------------------+
     |                   t                    t2 |
     |-------------------------------------------|
  1. | 22-12-1999 01:28:21   22-12-1999 01:00:00 |
  2. | 22-12-1999 01:29:21   22-12-1999 01:00:00 |
  3. | 22-12-1999 01:30:21   22-12-1999 02:00:00 |
  4. | 22-12-1999 01:31:21   22-12-1999 02:00:00 |
  5. | 22-12-1999 01:32:21   22-12-1999 02:00:00 |
     +-------------------------------------------+