@Rakesh93:
If you don't mind guessing the number of needed columns beforehand, the algorithm is simple:
data have ;
input txt $80. ;
cards ;
The time has come, the Walrus said, to talk of many things:
Of shoes and ships and sealing-wax, of cabbages and kings
And why the sea is boiling hot and whether pigs have wings
;
run ;
%let w = 20 ; * change to 200 in your case ;
data want (drop = _:) ;
set have ;
array c $ &w col1-col5 ;
_i_ = 1 ;
do _x = 1 to countw (txt, "") ;
c = catx (" ", c, scan (txt, _x, "")) ;
if length (c) <= &w and length (c) + length (scan (txt, _x + 1)) > &w or _x = countw (txt, "") then _i_ + 1 ;
end ;
run ;
If you want it to be truly dynamic, run the same exact algorithm twice: (1) to determine the actual dimension of array C and (2) to produce the final output, e.g.:
data _null_ ;
set have end = z ;
length c $ &w ;
do _x = 1 to countw (txt, "") ;
c = catx (" ", c, scan (txt, _x, "")) ;
if length (c) <= &w and length (c) + length (scan (txt, _x + 1)) > &w or _x = countw (txt, "") then do ;
_i_ = sum (_i_, 1) ;
call missing (c) ;
end ;
end ;
last._i_ = max (last._i_, _i_) ;
if z then call symputx ("d", last._i_) ;
run ;
data want (drop = _:) ;
set have ;
array c $ &w col1-col&d ;
_i_ = 1 ;
do _x = 1 to countw (txt, "") ;
c = catx (" ", c, scan (txt, _x, "")) ;
if length (c) <= &w and length (c) + length (scan (txt, _x + 1)) > &w or _x = countw (txt, "") then _i_ + 1 ;
end ;
run ;
You can massage it if you wish to encapsulate the repetitive code.
Kind regards
Paul D.
... View more