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.
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;
Get access to two SAS base certification prep courses and 150+ practice exercises
Two Certificate Prep Courses and 300+ Practice Exercises
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 consentThis 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 EnabledNecessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This 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-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This 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-others | 11 months | This 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-performance | 11 months | This 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_policy | 11 months | The 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 cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
PerformancePerformance 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.
AdvertisementAdvertisement 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.