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.
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.
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;
By default, the COMPRESS function removes leading, between and trailing spaces from a string.
data mydata2; set mydata; name2 = compress(name); run;
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.
Here we are deleting characters - at sign (@) and hash sign (#) from the "name" column.
data mydata2; set mydata; name2 = compress(name, '@#'); run;
data mydata2; set mydata; name2 = compress(name, '', 'a'); run;
Here we are keeping all alphabetical characters from a string and removing everything else.
data mydata2; set mydata; name2 = compress(name, '', 'ak'); run;
data mydata2; set mydata; name2 = compress(name, '', 'kd'); run;
data mydata2; set mydata; name2 = compress(name, '', 'd'); run;
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;
If you look at the second observation, both the capital and small letter 'S' or 's' were removed from name.
data mydata2; set mydata; name2 = compress(name, 's', 'k'); run;
data mydata2; set mydata; name2 = compress(name, '', 'l'); run;
data mydata2; set mydata; name2 = compress(name, '', 'u'); run;
data mydata2; set mydata; name2 = compress(name, '', 'p'); run;
Share Share Tweet