I see. You're not interested in items per order, but the number of days between orders (which may contain 1 or 100 items): proc sql noprint;
create table want as
select distinct a.MSTR_CUSTOMER_ID,
max(TRANSACTION_DATE) - min(TRANSACTION_DATE) as days,
b.orders_per_ID,
(max(TRANSACTION_DATE) - min(TRANSACTION_DATE))/b.orders_per_ID as average
from have as a, (select MSTR_CUSTOMER_ID,
count (distinct order_id) as orders_per_id
from have
group by MSTR_CUSTOMER_ID) as b
where a.MSTR_CUSTOMER_ID = b.MSTR_CUSTOMER_ID
group by a.MSTR_CUSTOMER_ID
having orders_per_id > 1
;
quit; Also, it looks like some of your counts are off. I see that ID 143541 has 3 distinct order ID's, while you have 4. Also you have 153 days for the difference for 54691, while there are only 139 days between 26NOV2017 and 10JUL2017.
... View more