Skip to Main Content

How can I get rid of extra spaces in a string variable? | SAS FAQ

Sometimes, a string variable can have many words in it and extra spaces
between the words. The easiest way to get rid of the extra spaces is to use SAS
function compbl.  Here is an example.

data test;
  length address1 $40. address2 $60.;
  input address1 $ 1-20 address2 $ 21-80;
  datalines;
  1234 Washington St     DC          12345
  1234 Irving St         Charlotte  NC      12345
  45 Wall     street     New           York  NY 90454
  ;
  run;
data test2;
  set test;
  address_compbl = compbl(address1)||compbl(address2);
run;
proc print data = test2;
run;
Obs         address1         address2

 1     1234 Washington St    DC          12345
 2     1234 Irving St        Charlotte  NC      12345
 3     45 Wall     street    New           York  NY 90454

Obs             address_compbl

 1     1234 Washington St DC 12345
 2     1234 Irving St Charlotte NC 12345
 3     45 Wall street New York NY 90454