UCLA Academic Technology Services HomeServicesClassesContactJobs

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

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

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.