Online File

How to use this page


Rick Aster: Professional SAS Programming Secrets: Contents

Chapter 14
Program 14p
Word wrap

Program | Alternate version of program for SAS 8


Program

data work.tenttextlines;
   drop text_length wrap_length line_length line_start ln c; 
   retain wrap_length 26; * Line length for word wrap;
   array textline{10} $ 26;
   set main.tent;
   text_length = lengthm(text);
   line_start = 1;
   do ln = lbound(textline) to hbound(textline);
      *
        Skip spaces.
      *;
      do line_start = line_start to text_length;
         if substrn(text, line_start, 1) ne ' ' then leave;
         end;
      *
        Find line length. Break at space or dash or after hyphen.
      *;
      do line_length = wrap_length to 1 by -1;
         c = line_start + line_length - 1;  * Index of last character;
         if c >= text_length or ln = hbound(textline) then leave;
         if substrn(text, c + 1, 1) = ' ' or substrn(text, c, 1) = ' '
             or substrn(text, c, 1) = '-' and substrn(text, c + 1, 1) ne '-'
             or substrn(text, c + 1, 2) = '--'
             then leave;
         end;
      if line_length = 0 then line_length = wrap_length;
      *
        Extract text line.
      *;
      textline{ln} = substrn(text, line_start, line_length);
      line_start + line_length;
      if line_start > text_length then leave;
      end;
run;
proc print data=work.tenttextlines (obs=2);
   var text textline1-textline4;
run;

Alternate version of program for SAS 8

*
  Professional SAS Programming Secrets
  Program 14p
  Word wrap
  
  Alternate version for SAS 8 - SUBSTR instead of SUBSTRN
*;

data work.tenttextlines;
   drop text_length wrap_length line_length line_start ln c; 
   retain wrap_length 26; * Line length for word wrap;
   array textline{10} $ 26;
   set main.tent;
   text_length = length(text);
   line_start = 1;
   do ln = lbound(textline) to hbound(textline);
      *
        Skip spaces.
      *;
      do line_start = line_start to text_length;
         if substr(text, line_start, 1) ne ' ' then leave;
         end;
      *
        Find line length. Break at space or dash or after hyphen.
      *;
      do line_length = wrap_length to 1 by -1;
         c = line_start + line_length - 1;  * Index of last character;
         if c >= text_length or ln = hbound(textline) then leave;
         if substr(text, c + 1, 1) = ' ' or substr(text, c, 1) = ' '
             or substr(text, c, 1) = '-' and substr(text, c + 1, 1) ne '-'
             or substr(text, c + 1, 2) = '--'
             then leave;
         end;
      if line_length = 0 then line_length = wrap_length;
      *
        Extract text line.
      *;
      textline{ln} = substr(text, line_start);
      textline{ln} = substr(textline{ln}, 1, line_length);
      line_start + line_length;
      if line_start > text_length then leave;
      end;
run;
proc print data=work.tenttextlines (obs=2);
   var text textline1-textline4;
run;

 O /\

Global
Statements

RICK ASTER

SAS

BOOKS

Tech | Dictionary

Download | Rastinate

Rick Aster

Professional SAS Programming Secrets

Contents/Online Files

Corrections