5 SAS Functions to Remove Blanks

5 SAS Functions to Remove Blanks

By : -

5 SAS Functions to Remove Blanks

Whether it is leading blank, trailing blank or multiple blanks, blanks are part of the basic topics to know about when dealing with character variables. Here are 5 functions you should be familiar with.

1. Removing Leading and Trailing Blanks using the strip Function,…

The strip function remove leading and trailing blanks saving us from using the old syntax trim(left()).

When you want to group several strings in a single one (concatenate), think about the  cat, cats, catt and catx functions. For more detail on this, have a look at the concatenation section.

In this example the leading blank before Hello and the trailing blank after Boo are removed.

data one;
   length before after1a after1b $25;
   before  = ' Hello  My Name is Boo ';
   after1a = strip(before);
   after1b = 'Hello My Name is Boo';
run;

2. Keep a Single Blank between Words using the compbl Function

Most of the time we only need a single space between two words in a string. The compbl function removes multiple blanks.

In the following example one of the two blanks is removed between the word Hello and the word My.

data one;
   length before after1a after1b $25;
   before  = 'Hello  My Name is Boo';
   after1a = compbl(before);
   after1b = 'Hello My Name is Boo';
run;

3. Removing Blanks using the compress Function

By defaut the compress function removes all blanks in a string. You just need to specify the characters to be removed as second parameter of the function to remove them instead.

In this first call of the compress function, all blanks are removed.  In the second call, blanks, l and o letters are removed from the string.

data one;
   length before after1a after1b after2a after2b $25;
   before  = 'Hello My Name is Boo';
   after1a = compress(before);
   after1b = 'HelloMyNameisBoo';
   after2a = compress(before,' lo');
   after2b = 'HeMyNameisB';
run;

Since SAS® 9.2 an additional parameter is available. It is called the modifier. It doesn’t apply on blanks.

  • k = keep instead of drop (exception pour les blancs)
  • i = keep or drop whether it is uppercase or lowercase
  • a = add alphabetic letters to the list of characters to consider
  • d = add digits to the list of characters to consider
  • n = add digit, underscore, alphabetic letters to the list of characters
  • p = add punctation mark to the list of characters
  • s = add blanks to the list of characters i.e. non printable characters such as return carriage, horizontal tab, vertical tab, line feed and form feed.

Here is another example. In the first call, the letter o will be kept because of the k letter in the first function parameter. In the second call, all m letters are kept whether they are uppercase or not. k indicates that we keep the letter and i that the case does not matter.

data one;
   length before after3a after3b $25;
   before  = 'Hello My Name is Boo';
   after3a = compress(before,'o',k);
   after3b = 'oBoo';
   after4a = compress(before,'m',ki);
   after4b = 'Mm';
run;

4. Avoid a Blank after Removing a Character from a String using transtrn and trimn Function

You can use tranwrd and translate unctions to changer a substring in a main string. For example we can change B to 1 in  ABC giving A1C . But what happen if we want to remove the B using the tranwrd and translate functions? A blank will remain: ABC becomes A C. To remove the blank, you can use this syntax:

data _null_;
   length x y $3;
   x='ABC';
   y=transtrn(x,'B',trimn(''));
   put x=;
   put y=;
run;

The trimn function compared to the trim function know how to deal with a length zero. The transtrn function also allow having a length zero as third parameter.

Check the log to see the result.

5. Delete the x First Blanks using rxparse and call rxchange

First, define the value before and after in a variable using the rxparse. function. In the example original blanks are replace to nothing.

Second, define the 3 or 4 parameters of call rxchange :

  1. The name of the variable containing the rxparse function.
  2. The number of characters to update. You can use the lengthn or the lengthc functions. Both functions can deal with a length of zero. The lengthn function includes trailing blanks in the computation whereas the lengthc function ignore trailing blanks.
  3. The name of the variable to update.
  4. By default the original variable is updated. To create a new variable, you need a forth parameter. Think about defining the length of this new variable.
data one;
   length after1a after1b $30;
   before  = ' ZZZ ABCD AB ';
   rx=rxparse("' ' to ");
   call rxchange (rx,lengthc(before),before,after1a);
   after1b ='ZZZABCDAB';
run;

 

Leave a Reply

Your email address will not be published.

nineteen − 7 =