Этот код будет обрабатывать несколько товаров, купленных в этот день. Я использовал Retain
Функциональность и вел подсчет подгрупп.
Данные: в тот же день я добавил два дополнительных предмета в A & B
data have;
infile datalines dlm=',' dsd;
informat Transaction_Date yymmdd10.;
format Transaction_Date yymmdd10.;
input Transaction_Date Customer $ Product $ Quantity Purchase_Value;
datalines;
2018-01-15, A , Milk , 1 , 100
2018-01-28, A , Onion , 2 , 140
2018-01-28, A , corn , 2 , 140
2018-02-13, B , Carrot , 1 , 50
2018-03-20, B , Rice , 10 , 40
2018-03-20, B , tomato , 10 , 40
2018-04-14, B , Carrot , 1 , 50
2018-06-02, C , Candy ,5 , 125
;
run;
Код: Счетчик - ваш N-й ряд
proc sort data=have; by Customer Transaction_Date; run;
data want;
set have;
by Customer Transaction_Date ;
retain outter;
retain inner;
retain counter;
if first.Customer then do; outter=1; counter=0; end; else outter+1;
if first.Transaction_Date then do; inner=1; counter+1; end; else inner+1;
if counter=2 then output;
run;
Выход:
Transaction_Date=2018-01-28 Customer=A Product=Onion Quantity=2 Purchase_Value=140 outter=2 inner=1 counter=2
Transaction_Date=2018-01-28 Customer=A Product=corn Quantity=2 Purchase_Value=140 outter=3 inner=2 counter=2
Transaction_Date=2018-03-20 Customer=B Product=Rice Quantity=10 Purchase_Value=40 outter=2 inner=1 counter=2
Transaction_Date=2018-03-20 Customer=B Product=tomato Quantity=10 Purchase_Value=40 outter=3 inner=2 counter=2