Here is one way to get the desired results:
data have;
input id Visit AE_free_text $;
cards;
1 1 1,4
2 1 1
3 1 2,4
4 1 1
5 1 1
6 1 3,9
7 1 3
8 1 2
;
run;
data _null_;
retain max 0 temp;
set have;
if _n_=1 then temp=countw(AE_free_text,',');
else do;
tempb=countw(AE_free_text,',');
max=max(temp,tempb,max);
end;
call symputx('total',max);
run;
data want(drop=ae_free_text i);
set have;
array text(*) text1-text&total;
do i=1 to dim(text);
text(i)=input(scan(ae_free_text,i,','),8.);
end;
run;
proc print;
run;
proc transpose data=want out=want1(drop=_name_ where=(ae_num1 ne .)) prefix=ae_num;
/* use the new variable name in the WHERE= clause */
by id visit;
var text:;
run;
proc print data=want1;
run;