Return to Homepage

Goto the Tip of the Month Archive

Other interesting pages ...
LinkedIn Profile
SAS Cheat Sheet
Useful SAS Code
Full SAS Example
Basic Statistics
Contact Information

SAS Tip of the Month
January 2016
(for SAS)

Sometimes in a text string we are processing there is a tab, linefeed or carriage return character (or characters) in the text. Removing them can be challenging, but here is a piece of code that may help:

   * Check each byte in string until no more
     non-printable characters are found. A
     non-printable character is defined outside
     the ASCII set 32 to 126.;
   do i=1 to lengthn();
      if not(32<=rank(substr(,i,1)<=126) then
       substr(,i,1)=' ';

There is another way that is usually seen in references on the subject that use the NOTPRINT function that is quicker to process as it only does a subsitution if a non-printable character is found. For the purposes of this function the results depend directly on the translation table that is in effect (see TRANTAB= System Option in SAS National Language Support (NLS): Reference Guide) and indirectly on the ENCODING and LOCALE system options:

   do until(_flg=0);

      *If a non-printable is found, replace with a space;
      if _flg>0 then substr(,_flg,1)=' ';

Hope this is useful.

Updated January 2, 2016