I'm using Teradata SQL Assistant for an analysis on date variables. Basically I have individuals that receive a treatment on a given date. These individuals might receive the treatment several times a year across several years but when there is a gap of more than 12 months I effectively treat this as a division. That is, I need to take the minimum date and the maximum date of treatment before the gap of 12 months occurred. If an individual then goes onto receive the treatment again the same process is followed discounting the previous gap of 12 months.
As a sample of the table I am working with:
Individual_ID | Treatment | Date Treatment Received | Time_Between_Treatment(Months) |
---|---|---|---|
A | B | 01/01/2010 | ? |
A | B | 01/02/2010 | 1 |
A | B | 01/04/2010 | 2 |
A | B | 01/05/2011 | 13 |
A | B | 01/06/2011 | 1 |
A | B | 01/08/2011 | 2 |
A | B | 01/09/2012 | 13 |
A | B | 01/10/2012 | 1 |
B | C | 01/01/2010 | ? |
B | C | 01/02/2010 | 1 |
B | C | 01/04/2010 | 2 |
B | C | 01/05/2011 | 13 |
B | C | 01/06/2011 | 1 |
B | C | 01/08/2011 | 2 |
B | C | 01/09/2012 | 13 |
B | C | 01/10/2012 | 1 |
In the above table Individual A and B would have 3 rows in the final output:
Individual_ID | Treatment | Minimum Date | Maximum Date |
---|---|---|---|
A | B | 01/01/2010 | 01/04/2010 |
A | B | 01/06/2011 | 01/08/2011 |
A | B | 01/10/2012 | ? |
B | C | 01/01/2010 | 01/04/2010 |
B | C | 01/06/2011 | 01/08/2011 |
B | C | 01/10/2012 | ? |
My end goal is to take an average of the months between treatments but as I was unable to achieve this I think the above output would be the next best thing as I can start adding the average for each row and then continue my analysis.
I have attempted using various techniques involving Window Functions most notably AVG () OVER (PARTITION BY ... ORDER BY ...). In addition I have also tried using flags to separate the data into below and above 12 months but have not been able to compute the desired output since then.
I have access to SAS if anyone thinks this would be easier to achieve using this software.
Thanks in advance