I am using Stata 17 and would like to get your advice on Stata code that almost solves my problem of choosing the correct of 2 panel data recordings in the same minute to keep and which to delete. I collapsed a data file with panel data recorded every 30 seconds but have 158 instances where the collapsed database now included recordings for the same 1 minute but in 2 locations (e.g. if the person went from their home to the outdoors during time 08:01 hours then I have a record for home at 8:01 and outdoors also at 8:01). I would like to keep the record for the first location (home in the example above).
This code creates a flag for any recording during the same 1 minute (with the 6 variables ID, Session, year, doy, hr and min all being equal):
And then the following code for the location variable named timeactivity (integer variable) to note change in location in each of the prior 3 observations:
Including some data below - see the 2 entries at minute 30 as well as at minute 35.
Thank you in advance for your help! Best, Leslie
This code creates a flag for any recording during the same 1 minute (with the 6 variables ID, Session, year, doy, hr and min all being equal):
Code:
duplicates tag ID Session year doy hr min, generate(flag_dupe) tab flag_dupe
Code:
sort IDnum Session year doy hr min bysort IDnum Session: gen int flag_ta = timeactivity[_n] - timeactivity[_n-1] bysort IDnum Session: gen int flag_ta2 = timeactivity[_n] - timeactivity[_n-2] bysort IDnum Session: gen int flag_ta3 = timeactivity[_n] - timeactivity[_n-3]
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str7 ID double Session float(year doy hr min timeactivity) byte flag_dupe int(flag_ta flag_ta2 flag_ta3) "Kat 005" 2 2020 32 14 7 6 0 0 0 0 "Kat 005" 2 2020 32 14 8 6 0 0 0 0 "Kat 005" 2 2020 32 14 11 6 0 0 0 0 "Kat 005" 2 2020 32 14 12 6 0 0 0 0 "Kat 005" 2 2020 32 14 30 1 1 -5 -5 -5 "Kat 005" 2 2020 32 14 30 6 1 5 0 0 "Kat 005" 2 2020 32 14 32 1 0 -5 0 -5 "Kat 005" 2 2020 32 14 35 1 1 0 -5 0 "Kat 005" 2 2020 32 14 35 4 1 3 3 -2 "Kat 005" 2 2020 32 14 36 4 0 0 3 3 end label values timeactivity tactive label def tactive 1 "Car", modify label def tactive 4 "Home", modify label def tactive 6 "Outdoors", modify
Comment