SAS | COMPRESS Function with Examples
COMPRESS function is basically used to compress/removes all the spaces/blanks in a character string. In other words, it removes leading, between and trailing spaces from the strings.
The COMPRESS function allows null arguments. A null argument is treated as a string that features a length of zero.
Example:
Input: " Geeks For Geeks " Output: "Geeksforgeeks" Input: null Output: null
Syntax:
COMPRESS(String, characters to be removed, Modifier)
Program:
- Sample dataset:
Datachar;InputName$ 1-50 ;Cards;GeeksforGeeksGeeksforGeeksGe eksforGeeksGeeks forGeeks;Run;chevron_rightfilter_none - Using COMPRESS function on sample data defined above:
Data char1;Setchar;char1 = compress(Name);run;chevron_rightfilter_none - Remove specific characters
In SAS, the additional parameter referred to as MODIFIER was added to the function.
The following keywords can be used as modifiers-
- a – Remove all upper and lower case characters from String.
- ak – Keep only alphabets from String.
- kd – Keeps only numeric values
- d – Remove numerical values from String.
- i – Remove specified characters both upper and lower case from String.
- k – keeps 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 default.
- u – Remove uppercase characters from String.
- Example 1: Compressing Lowercase Letters
data _null_;x='456-123-852 A 123-8910 c';y=compress(x,'ABCD','l');put string=;run;chevron_rightfilter_noneOutput
string= 456-123-852 123-8910
- Example 2: Compressing Space Characters
data one;x='1 9 3 4 5 6 8';y=compress(x,,'s');put string=;run;chevron_rightfilter_noneOutput
string= 1934568
- Example 3: Keeping Characters in the List
data one;x='Chemistry A Maths B Physics C';y=compress(x,'ABCD','k');put string=;run;chevron_rightfilter_noneOutput
string= ABC
- Example 4: Compressing a String and Returning a Length of 0
data _null_;x='';len=lengthn(compress(x));put len=;run;chevron_rightfilter_noneOutput
len= 0
Examples:
Recommended Posts:
- SAS | COALESCE Function with Examples
- Arrow operator -> in C/C++ with Examples
- PImpl Idiom in C++ with Examples
- Java Swing | JDialog with examples
- Java Swing | JCheckBox with examples
- Java Swing | JComboBox with examples
- Java Swing | JSplitPane with Examples
- Difference between Functional Programming and Object Oriented Programming
- Program to implement Logic Gates
- Which Programming Language Should I Choose as a Beginner?
- Arrow operator -> in C/C++ with Examples
- 12 Reasons Why You Should Learn Python in 2019
- 5 Most Difficult Programming Languages of the World
- Object Oriented Programming in Ruby | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





