Master SAS in 30 days!

A jargon-free, easy-to-learn SAS base course that is tailor-made for students with no prior knowledge of SAS.

Compress Function: How to Remove Blank Spaces or Selected Characters From a Character Value

The COMPRESS function is often used to remove blank spaces from a character value.

Let's look at some examples.

The data set above contains a single character value.

Remove Blank Spaces from Character Value
 
You can use the COMPRESS function to remove all of the blank spaces in a character value.

data string2;
set string;
comp = compress(text);
run;

The COMPRESS function compresses the character value and removes all of the blank spaces from the string.

Remove Selected Characters from Character Value

You can also specify a character to be removed in the COMPRESS function.

Example

data string2;
set string;
comp = compress(text, 'a');
run;

The character 'a' is specified in the second parameter of the COMPRESS function. It is removed from the text.

Do you have a hard time learning SAS?

Take our Practical SAS Training Course for Absolute Beginners and learn how to write your first SAS program!

Remove Multiple Characters from Character Value

Multiple characters can be removed simultaneously in the COMPRESS function.

Example

data string2;
set string;
comp = compress(text, 'abc');
run;

The 3 letters 'a', 'b', and 'c' are specified in the second parameters.

All 3 letters are removed from the text.

Please note that the capital letters 'A', 'B' and 'C' remain in the text. You can add a modifier in the third parameter to ignore case of the characters to be removed.

Ignore Case of Characters To Be Removed

Adding the 'i' modifier to the third parameter of the COMPRESS function tells SAS to ignore the case of the characters when removing them from the text.

Example

data string2;
set string;
comp = compress(text, 'abc', 'i');
run;

In this example, all 6 letters of 'A', 'B', 'C', 'a', 'b', and 'c' are removed from the string.

Remove All Alphabetic Characters
 
You can use the 'a' modifier to remove all of the alphabetic characters from the text.

data string2;
set string;
comp = compress(text, '', 'a');
run;

In addition to the blank space specified in the second parameter, the 'a' modifier in the third parameter tells SAS to remove all of the alphabetic characters from the text.

Remove All Digits
 
Similar to the 'a' modifier, the 'd' modifier removes all of the digits from the text.

data string2;
set string;
comp = compress(text, '', 'd');
run;

Become a Certified SAS Specialist

Get access to two SAS base certification prep courses and 150+ practice exercises

Master SAS in 30 Days

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
iconmail

Get latest articles from SASCrunch

SAS Base Certification Exam Prep Course

Two Certificate Prep Courses and 300+ Practice Exercises