Hi, I have a question about checking days overlap. I have a data set listed as below. For the same id, there might be more than one drug prescription (variable: drug). For each drug prescription, there may be refill (variable: refill, 0: first time, 1: first refill, 2: second refill.....n: nth refill). There are two variables for beginning date and ending date for prescription. There are more than 10K different id. id drug refill begin_date end_date 1 drug1 0 03/01/14 03/10/14 1 drug1 1 03/11/14 03/20/14 1 drug1 2 03/25/14 04/05/14 1 drug2 0 03/02/14 03/09/14 1 drug2 1 03/17/14 03/24/14 1 drug2 2 03/27/14 04/04/14 1 drug3 0 09/02/14 09/09/14 2 2 2 3 3 3 . . . . n What I need to do is to check if the overlap days for different drug for the same person are more than 5 days. In another word, I defined overlap as two different drugs for more than 5 days together. What I plan to do is listed as below. (step 1) to check the begin_date and end_date for the same drug. If the begin_date of refill is just the next day to the end_date of last time, then combine the date into one row. Actually I do not care the refill variable by the end. so I want my original data to be changed like below. For example, for drug1 of id=1, the first two rows will be combined into one row, because 03/11/14 is just next day to 03/10/14. I am not sure how to do this. id drug refill begin_date end_date 1 drug1 0 03/01/14 03/20/14 1 drug1 2 03/25/14 04/05/14 1 drug2 0 03/02/14 03/09/14 1 drug2 1 03/17/14 03/24/14 1 drug2 2 03/27/14 04/04/14 1 drug3 0 03/02/14 03/09/14 2 2 2 . . . n n (step2) after step 1, I am not sure how to compare the day period with each other for different drugs within the same id. The first difficulty is I do not want to compare the day period for the same drug (like row 1 and row 2 for drug1). The second difficulty is how to do pairwise comparison among different drugs with each other within the same id by iteration. Finally, I need create dummy variable (overlap) indicating if there are two different drugs overlap more than 5 days for each id, like below. id overlap 1 1 2 1 3 0 . . . Many thanks in advanced for your answer!
... View more