I am building a program to assess rolling client retention

I am building a program to assess rolling client retention. Periods are defined as 1 year, moving forward by one month. (Jan 2012-Jan 2013, Feb 2012-Feb2013…) If a client purchased in period 1 and period 2, he is retained. If a client purchased in period 1, but not period 2, he is not retained. If a client purchased in period 2, but did not purchase in period 1, he is a new client. To do this, I need to construct left, inner and right joins for an arbitrarily large number of dates and clients. For example, I want to be able to look at the change in client retention for period 41 to period 42. As you can see by my attached SAS program, I have painstakingly accomplished this task by writing 2500 lines of code. Now I need to make it more efficient. There are only two variables in the input files: date and customer number.data data1;input cust_num $ invoicedate : mmddyy10.;month=month(invoicedate);format invdate mmddyy10.;datalines;a 1/1/2012a 1/1/2012a 2/1/2012b 1/1/2012b 3/1/2012c 2/1/2012d 2/6/2012d 2/11/2012;run;