Hello EmestoC,
Thsi is a possible solution, may be not an optimal one...
[pre]
data i;
infile datalines delimiter="*'";
input ID PROD D1 $ D2 $ AMOUNT CAT $;
date1=MDY(SUBSTR(d1,5,2),SUBSTR(d1,7,2),SUBSTR(d1,1,4));
date2=MDY(SUBSTR(d2,5,2),SUBSTR(d2,7,2),SUBSTR(d2,1,4));
format date1 date2 date7.;
drop d1 d2;
datalines;
10***111****20050108**20050110****100*******'B'
10***111****20050112**20050117****500*******'A'
10***111****20050121**20050124****200*******'B'
10***222****20050110**20050112****150*******'B'
10***222****20050115**20050120****250*******'B'
20***222****20010601**20010616****700*******'B'
;
run;
/* CAT */;
proc sort data=i(keep=id cat where=(Cat='A')) out=c;
by id;
run;
/* AMT */;
proc SQL;
create table a as
select ID, Prod, SUM(AMOUNT) as AM, count(*) as N
from i
group by ID, Prod
;quit;
/* DAYS */;
proc sort data=i out=i;
by id prod date1;
run;
data d;
retain dt1;
set i;
if first.prod then dt1=date1;
if last.prod then do; d=date2-dt1; output; end;
by ID prod;
keep ID prod date1 date2 d;
run;
/* Prods into macro variables */;
proc SQL;
select distinct prod into :p1-:p2 from i
%let p1=%trim(&p1);
%let p2=%trim(&p2);
;quit;
/* MERGE */;
data da;
merge d a;
by id prod;
run;
/* TRANSPOSE date1 date2 d and am */;
proc transpose data=da out=d1 prefix=d1_;
var date1;
id prod;
by id;
run;
proc transpose data=da out=d2 prefix=d2_;
var date1;
id prod;
by id;
run;
proc transpose data=da out=days prefix=d_;
var d;
id prod;
by id;
run;
proc transpose data=da out=am prefix=am_;
var am;
id prod;
by id;
run;
data r0;
retain ID STRT1 STP1 D1 AM1 STRT2 STP2 D2 AM2;
merge d1 (rename=(d1_&p1=STRT1 d1_&p2=STRT2))
d2 (rename=(d2_&p1=STP1 d2_&p2=STP2 ))
days(rename=(d_&p1 =D1 d_&p2 =D2 ))
am (rename=(am_&p1=AM1 am_&p2=AM2 ));
if STRT1=. and STP1=. and D1=. and AM1=. then do;
STRT1=STRT2; STP1=STP2; D1=D2; AM1=AM2;
STRT2=. ; STP2= .; D2= .; AM2= .;
end;
by id;
drop _name_;
run;
/* Add CAT */;
proc SQL;
create table r as
select a.*,
case
when b.cat="A" then "A"
else "B"
end as CAT
from r0 as a left join c as b
on a.ID=b.ID
;quit;
[/pre]
Sincerely,
SPR