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.

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.

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.

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

Article Rating 0 Comments Newest Most Voted Inline Feedbacks View all comments

Get latest articles from SASCrunch

SAS Base Certification Exam Prep Course

Two Certificate Prep Courses and 300+ Practice Exercises

Copyright © 2012-2024 SASCrunch.com All rights reserved.

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.

Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.

Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.