How to Use the COMPBL Function in SAS (With Examples)

You can use the COMPBL function in SAS to compress multiple blanks in a character string into a single blank.

The following example shows how to use the COMPBL function in practice.

Example: How to Use COMPBL Function in SAS

Suppose we have the following dataset in SAS that contains the names of various people:

/*create dataset*/
data original_data;
    input name $char30.;
Andy   Douglas
James Mike      Thomas
Arthur   McNeely   Stevenson
Jake Smith
Arnold Walker
Graham    Johnson
Grant  Beeson

/*view dataset*/
proc report data=original_data;
    define name / display style=[asis=on];

Note: We used PROC REPORT with the option DISPLAY STYLE=[ASIS=ON] to force SAS to display multiple blanks in the output.

Notice that some of the names have multiple blanks in between the individual words.

We can create a new dataset in which we use the COMPBL function to compress multiple blanks in each name into single blanks:

/*create new dataset*/
data new_data;
    set original_data;
    compbl_name = compbl(name);

/*view new dataset*/
proc report data=new_data;
    define name / display style=[asis=on];

COMPBL function in SAS example

Notice that each of the names now only have single blanks.

The COMPBL function compressed all multiple blanks into single blanks.

Note that this is different than the COMPRESS function in SAS, which can be used to remove blanks entirely from strings.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

SAS: How to Remove Commas from String
SAS: How to Replace Characters in a String
SAS: How to Remove Special Characters from Strings

Leave a Reply

Your email address will not be published. Required fields are marked *