Hi All,
In the attached example dataset, I have workers (worker_id) who deliver orders from stores (store_id). Each worker can deliver orders from multiple stores. I want to create a new variable "gaps" that calculates how many orders were picked in between 2 orders that were picked from the same store. For example, in the sample data provided below, worker no. 59 picks an order from store no. 5330. After that the worker picks 2 orders from different stores (5941 and 163) and then again picks from store 5330. So, the variable "gap" is 2 for the 4th item in the same data. Since for the 1st 3 items we do not know how long were the gaps before this, those are denoted as missing (.)
The result for each column should be something like: . , . , . , 2 , . , 2 , 4 , 0 , 0 , and so on. Gaps are shown as 0 if the previous order was picked from the same store.
Thank you!
In the attached example dataset, I have workers (worker_id) who deliver orders from stores (store_id). Each worker can deliver orders from multiple stores. I want to create a new variable "gaps" that calculates how many orders were picked in between 2 orders that were picked from the same store. For example, in the sample data provided below, worker no. 59 picks an order from store no. 5330. After that the worker picks 2 orders from different stores (5941 and 163) and then again picks from store 5330. So, the variable "gap" is 2 for the 4th item in the same data. Since for the 1st 3 items we do not know how long were the gaps before this, those are denoted as missing (.)
The result for each column should be something like: . , . , . , 2 , . , 2 , 4 , 0 , 0 , and so on. Gaps are shown as 0 if the previous order was picked from the same store.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long worker_id int(store_id date_pickstart) str11 courier_store 59 5330 22237 "59-5330" 59 5941 22237 "59-5941" 59 163 22238 "59-163" 59 5330 22238 "59-5330" 59 5051 22238 "59-5051" 59 163 22239 "59-163" 59 5941 22239 "59-5941" 59 5941 22239 "59-5941" 59 5941 22240 "59-5941" 59 163 22240 "59-163" 59 5330 22240 "59-5330" 59 5941 22240 "59-5941" 59 5330 22241 "59-5330" 59 5941 22242 "59-5941" 59 5941 22243 "59-5941" 59 5284 22243 "59-5284" 59 5941 22243 "59-5941" 59 5941 22244 "59-5941" 59 5941 22244 "59-5941" 59 163 22246 "59-163" end format %td date_pickstart
Comment