83 users online | 83 Guests and 0 Registered

How do I remove consecutive blanks from a text string?


While the COMPRESS function in SAS will remove, by default, every instance of the blank space, sometimes it is desirable to remove all-but-one of the blank spaces - such as when data has additional embedded blanks when un-trimmed strings have been concatenated.

Consider the code:

data names ;
  set sashelp.class (keep = name sex) ;
    if sex = 'M' then sal = 'Master' ;
	  else sal = 'Miss' ;
	surname = 'Smith' ;
	fullname = sal !! ' ' !! name !! surname ;
run ;

which generates the fullname variable in the form:

Master Alfred  Smith
Miss   Alice   Smith
Miss   Barbara Smith
Miss   Carol   Smith
Master Henry   Smith
Master James   Smith
Miss   Jane    Smith
Miss   Janet   Smith
Master Jeffrey Smith
Master John    Smith
Miss   Joyce   Smith
Miss   Judy    Smith
Miss   Louise  Smith
Miss   Mary    Smith
Master Philip  Smith
Master Robert  Smith
Master Ronald  Smith
Master Thomas  Smith
Master William Smith

Using the COMPBL function iteratively removes repeated blanks, but leaves the last one.

data names ;
  set sashelp.class (keep = name sex) ;
    if sex = 'M' then sal = 'Master' ;
	  else sal = 'Miss' ;
	surname = 'Smith' ;
	fullname = compbl(sal !! ' ' !! name !! surname) ;
run ;

which generates the more acptable result:

Master Alfred Smith
Miss Alice Smith
Miss Barbara Smith
Miss Carol Smith
Master Henry Smith
Master James Smith
Miss Jane Smith
Miss Janet Smith
Master Jeffrey Smith
Master John Smith
Miss Joyce Smith
Miss Judy Smith
Miss Louise Smith
Miss Mary Smith
Master Philip Smith
Master Robert Smith
Master Ronald Smith
Master Thomas Smith
Master William Smith

Author:
Alan D Rudland
Revision:
1.1
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.