data have;
input Id $;
cards;
a
b
c
a
b
a
b
c
b
;
/*Need this step for horizontal variables names*/
proc sql noprint;
select distinct id,count(distinct id) into :vars separated by ' ',:c
from have;
quit;
/*straight forward look up and count*/
data want;
set have end=lr;
array names(&c) &vars;
array temp(&c) $ _temporary_ ;
if _n_=1 then do _n_=1 to dim(temp);
temp(_n_)=vname(names(_n_));
end;
names(whichc(id,of temp(*)))+1;
if lr;
total=sum(of names(*));
output;
drop id;
run;