如何删除特定时间段内的第一行?

时间:2018-04-15 18:20:52

标签: for-loop time foreach stata

我有一个包含名称,日期和时间变量的表。我想做下面提到的操作:

如果9:00:01< = time< = 9:05:59则删除间隔中的第一行。如果下一行的时间值与此间隔中的第一行相同,则删除所有这些名称和日期变量。

以下是我的数据示例:

* Example generated by -dataex-. To install: ssc install dataex
clear
input str4 name long date str8 time
"A" 17659 "09:01:41"
"A" 17659 "09:01:41"
"A" 17659 "09:04:41"
"A" 17659 "11:32:41"
"A" 17660 "09:06:00"
"A" 17660 "09:06:01"
"A" 17660 "12:32:41"
"B" 17659 "09:00:01"
"B" 17659 "09:00:01"
"B" 17659 "11:33:41"
"B" 17661 "09:05:59"
"B" 17661 "11:35:41"
"B" 17661 "11:36:41"
"B" 17661 "11:37:41"
"B" 17664 "11:27:41"
"B" 17664 "11:27:41"
end
format %d date

所以,结果是:

name    date    time
A   7-May-08    9:04:41
A   7-May-08    11:32:41
A   8-May-08    9:06:00
A   8-May-08    9:06:01
A   8-May-08    12:32:41
B   7-May-08    11:33:41
B   9-May-08    11:35:41
B   9-May-08    11:36:41
B   9-May-08    11:37:41
B   12-May-08   11:27:41
B   12-May-08   11:27:41

我该怎么做?

1 个答案:

答案 0 :(得分:1)

bysort name date : generate todrop = inrange(clock(time, "hms"), clock("09:00:00", "hms"), ///
clock("09:05:59", "hms")) ///
& time == time[1]
drop if todrop == 1
drop todrop