Hai.kuo definitely lived up to his name here, but... this question is not marked as "answered" yet, so I thought I'd provide one more option (similar to DBailey) Assuming that Product_type is a character variable data have; infile datalines ; informat Purchase_Date date9.; format Purchase_Date date9.; length Transaction_Ref $5 Product_Type $10; input customer_number Purchase_Date Transaction_Ref Product_Type ; datalines; 1 16JUL2014 12345 TYPE1 1 17JUL2014 12346 TYPE1 1 18JUL2014 12347 TYPE1 1 18JUL2014 12347 TYPE2 1 18JUL2014 12347 TYPE3 1 19JUL2014 12347 TYPE1 2 16JUL2014 22345 TYPE1 2 17JUL2014 22346 TYPE1 2 18JUL2014 22347 TYPE1 2 18JUL2014 22347 TYPE2 2 18JUL2014 22347 TYPE3 2 19JUL2014 22347 TYPE1 ; run; proc sql; create table want as select a.customer_number ,a.Purchase_Date ,a.Transaction_Ref ,a.Product_Type ,count( DISTINCT (case when b.purchase_date between a.purchase_date-365 and a.purchase_date-1 then b.product_type else '' end) ) as type_ct from have A left join have B on a.customer_number = b.customer_number group by 1,2,3,4 ;quit;
... View more