Using a regular expression:
data have;
text = "Very long text with a lot of words, which should be split along word boundaries into chunks of a given size";
run;
%let chunk = 50; /* length of individual pieces */
data want;
set have;
length part $ &chunk.;
retain rx;
drop rx;
if _n_ = 1 then do;
/* Don't add a space after the comma in the following statement! */
rx = prxparse("/(.{0,&chunk.}\b)/");
end;
begin = 1;
end = -1;
pos = 0;
len = 0;
call prxnext(rx, begin, end, trim(text), pos, len);
do while (pos > 0 and len> 0);
part = substr(text, pos, len);
output;
call prxnext(rx, begin, end, trim(text), pos, len);
end;
run;
... View more