可以使用clock函数,计算的差值为毫秒,转换为天数、小时或分钟即可。演示命令如下:
clear
input id str9 (sleep_date sleep_time wake_date wake_time)
1 11jan2019 2154 12jan2019 0716
2 12jan2019 0017 12jan2019 0934
3 12jan2019 0314 12jan2019 0817
4 11jan2019 2354 12jan2019 0730
end
gen double wanted_ms = clock(wake_date + wake_time, "DMY hm") - clock(sleep_date + sleep_time, "DMY hm")
gen wanted_hr = wanted_ms / (60 * 60 * 1000)
list
结果如下:
+------------------------------------------------------------------------+
| id sleep_d~e sleep~me wake_date wake_t~e wanted~s wanted~r |
|------------------------------------------------------------------------|
1. | 1 11jan2019 2154 12jan2019 0716 33720000 9.366667 |
2. | 2 12jan2019 0017 12jan2019 0934 33420000 9.283334 |
3. | 3 12jan2019 0314 12jan2019 0817 18180000 5.05 |
4. | 4 11jan2019 2354 12jan2019 0730 27360000 7.6 |
+------------------------------------------------------------------------+
完,本文内容交流请移步:http://www.epiman.cn/thread-153085-1-1.html