data a;
input id week amount;
cards;
1 2 56
1 2 56
1 2 7
1 4 656
1 4 34
1 6 33
1 7 34
2 1 43
2 1 3
2 1 3
2 4 3
2 5 2
2 5 234
3 2 3
3 2 22
3 2 34
3 7 4
3 7 32
;
run;
data b;set a;
/*
if first.id then n=0;
if first.id & first.week then n=1;
retain n;
if ^first.id & first.week then n+1;
by id week;
run;
data b;set b;
*/
if first.week then m=0;
m+1;
by id week;
run;
data b;set b end=lst;
if _n_=1 then max=m;
retain max;
if m>max then max=m;
if lst then call symput('max',left(max));
drop max;
run;
%put &max;
data ord;set a;
keep week;
run;
proc sort data=ord nodupkey;by week;run;
proc sql;select cat('week',week) into: wk separated by "," from ord;quit;
%put &wk;
%macro trans;
%do i=1 %to &max;
data dsn;set b;
if m=&i;
run;
proc transpose data=dsn out=dsn1 prefix=week;
by id m;id week;var amount;run;
proc datasets lib=work;append base=t force data=dsn1;quit;
%end;
proc sql;create table res as select id,&wk from t order by id,m;quit;
%mend trans;
%trans;