Search code examples
for-looptimeforeachstata

How to remove first rows in a specific period of time?


I have a table which has name, date and time variables. I want to do the operation which is mentioned below:

If 9:00:01 <= time <= 9:05:59 then delete the first row in the interval. If a time value of next rows is the same with first row in this interval then delete all of them for that name and date variables.

Here is an example of my data:

* 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

So, the result is:

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

How can I do that?


Solution

  • 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