I created a data step macro to split a long string into a fixed length but also maintain text readability by splitting at the space closest to the fixed length. For example, a comment has 113 characters. To split it into 4 pieces each with max of 30 characters. The macro works, but I don't know how to simplify &Var.1, &Var.2 etc. There should be a simple way to say like else &var.i=.... Below is the macro and the test.
%macro split_text(txt=,var=);
length __txt $2000;
__txt=&txt;
m=30;
len=length(__txt);
n=floor(len/m);
do i=0 to n;
j=find(__txt,' ',-m);
if i=0 then &var=substr(__txt,1,j-1);
if i=1 then &var.1=substr(__txt,1,j-1);
if i=2 then &var.2=substr(__txt,1,j-1);
if i=3 then &var.3=substr(__txt,1,j-1);
if i=4 then &var.4=substr(__txt,1,j-1);
*reset;
__txt=substr(__txt,j+1);
end;
%mend;
data test;
comment = 'When splitting a text string into several records, the text should be split between words to improve readability.';
%split_text(txt=comment,var=COM);
run;
Below is the result.
... View more