5 SAS Functions to Remove Blanks
By : Admin_programmer -
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 remove leading and trailing blanks saving us from using the old syntax
When you want to group several strings in a single one (concatenate), think about the
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
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
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
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,
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
You can use
translate unctions to changer a substring in a main string. For example we can change B to 1 in
A1C . But what happen if we want to remove the B using the
translate functions? A blank will remain:
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;
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
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 :
- The name of the variable containing the
- The number of characters to update. You can use the lengthn or the
lengthcfunctions. Both functions can deal with a length of zero. The
lengthnfunction includes trailing blanks in the computation whereas the
lengthcfunction ignore trailing blanks.
- The name of the variable to update.
- 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;