Online File

Chapter 40 Program Word wrap

```data _null_;
retain text
"If you don't have a plan for your life, someone else does."
linelen 25;
length fragment \$ 3;
array textline{4} \$ 25;

c = 1;
length = length(text);

* Find lines of text. ;
do i = 1 to dim(textline);
textline{i} = '';
if c >= length then continue;

* Skip spaces to find start of line. ;
do while(substr(text, c, 1) = ' ');
c + 1;
end;
start = c;
* Find end of line at space, hyphen, or dash. ;
end = 0;
if start + linelen <= length then
do c = start + linelen - 1 to start by -1
until (end);
fragment = substr(text, c);
if substr(fragment, 2, 1) = ' ' or
substr(fragment, 2, 2) = '--' or
(substr(fragment, 1, 1) = '-' and
substr(fragment, 2, 1) ne '-') then end = c;
end;
if end then do;
textline{i} = substr(text, start, end - start + 1);
c = end + 1;
end;
else do;
textline{i} = substr(text, start);
c = start + linelen;
end;
end;

* Show results. ;
do i = 1 to dim(textline);
if textline{i} ne '' then put textline{i};
end;
run;
```