SAS COMPRESS Function: Learn with Examples

In this tutorial we will cover how to use the COMPRESS function in SAS, along with examples.

What does the COMPRESS Function do?

The COMPRESS function in SAS is used to remove specific characters from a string.

Syntax of COMPRESS Function

The syntax of the COMPRESS function is as follows:

COMPRESS(string, characters to be removed, modifier)
  • string: The string from which you want to remove characters.
  • characters to be removed: The characters that you want to remove from the original string.
  • modifier It is an optional argument. Specify the keyword for the action you want to perform.
List of Modifiers

Here is a list of modifiers in the COMPRESS function in SAS.

  • a – Remove all alphabetical characters from String.
  • ak - Keep only alphabets from String.
  • kd - Keep only numeric values from String.
  • d – Remove numerical values from String.
  • i – Remove specified characters (both upper and lower case) from String.
  • k – Keep the specified characters in the String instead of removing them.
  • l – Remove lowercase characters from String.
  • p – Remove punctuation characters from String.
  • s – Remove spaces from String. This is the default.
  • u – Remove uppercase characters from String.
Sample Dataset

The following code creates a sample SAS Dataset that will be used to demonstrate examples of the COMPRESS function.

data mydata;
input name $20.;
cards;
Sandy David 123.
Sam Andreas@14
123Sahil Arora
#Deeps Bhalla
;
run;
1. Remove All Spaces from String

By default, the COMPRESS function removes leading, between and trailing spaces from a string.

data mydata2;
  set mydata;
  name2 = compress(name);
run;
SAS: COMPRESS Function

The above code takes the existing dataset "mydata" and creates a new dataset "mydata2" with an additional variable "name2", where the values in "name2" have spaces removed using the COMPRESS function.

2. Remove Specific Characters from String

Here we are deleting characters - at sign (@) and hash sign (#) from the "name" column.

data mydata2;
  set mydata;
  name2 = compress(name, '@#');
run;
SAS: Remove Specific Characters from String
3. Remove all alphabets from string
data mydata2;
  set mydata;
  name2 = compress(name, '', 'a');
run;
SAS: Remove all alphabetical characters
4. Keep only alphabets from string

Here we are keeping all alphabetical characters from a string and removing everything else.

data mydata2;
  set mydata;
  name2 = compress(name, '', 'ak');
run;
SAS: Keep only alphabets from String
5. Keep only numeric values from string
data mydata2;
  set mydata;
  name2 = compress(name, '', 'kd');
run;
SAS: Keep only numeric values from String
6. Remove numerical values from string
data mydata2;
  set mydata;
  name2 = compress(name, '', 'd');
run;
SAS: Remove numerical values from String
7. Remove specified characters both upper and lower case from string

To remove specified characters from a string while ignoring case sensitivity, you can use the COMPRESS function in SAS with the 'i' modifier.

data mydata2;
  set mydata;
  name2 = compress(name, 's', 'i');
run;
SAS: Remove characters and ignoring case sensitivity

If you look at the second observation, both the capital and small letter 'S' or 's' were removed from name.

8. Keep the specified characters in the string
data mydata2;
  set mydata;
  name2 = compress(name, 's', 'k');
run;
9. Remove lowercase characters from string
data mydata2;
  set mydata;
  name2 = compress(name, '', 'l');
run;
10. Remove uppercase characters from string
data mydata2;
  set mydata;
  name2 = compress(name, '', 'u');
run;
11. Remove punctuation characters from string
data mydata2;
  set mydata;
  name2 = compress(name, '', 'p');
run;
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

0 Response to "SAS COMPRESS Function: Learn with Examples"

Post a Comment

Next → ← Prev