SAS | COMPRESS Function with Examples

29 Jul, 2019
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:Data char;Input Name $ 1-50 ;Cards;Geeks   for GeeksGeeksforGeeksGe   eks for  GeeksGeeks f   or  Geeks;Run;Sample Data Output:
Data char;Input Name $ 1-50 ;Cards;Geeks   for GeeksGeeksforGeeksGe   eks for  GeeksGeeks f   or  Geeks;Run;
Sample Data Output:
Using COMPRESS function on sample data defined above:Data char1;Set char;char1 = compress(Name);run;Output
Data char1;Set char;char1 = compress(Name);run;
Output
Remove specific charactersIn 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 valuesd – 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.Examples:Example 1: Compressing Lowercase Lettersdata _null_;   x='456-123-852 A 123-8910 c';   y=compress(x, 'ABCD', 'l');   put string=;run;Outputstring= 456-123-852 123-8910Example 2: Compressing Space Charactersdata one;   x='1 9 3 4 5 6 8';   y=compress(x,, 's');   put string=;run;Outputstring= 1934568Example 3: Keeping Characters in the Listdata one;   x='Chemistry A Maths B Physics C';   y=compress(x, 'ABCD', 'k');   put string=;run;Outputstring= ABCExample 4: Compressing a String and Returning a Length of 0data _null_;   x='';   len=lengthn(compress(x));   put len=;run;Outputlen= 0
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.
Examples:
Example 1: Compressing Lowercase Lettersdata _null_;   x='456-123-852 A 123-8910 c';   y=compress(x, 'ABCD', 'l');   put string=;run;Outputstring= 456-123-852 123-8910
data _null_;   x='456-123-852 A 123-8910 c';   y=compress(x, 'ABCD', 'l');   put string=;run;
Output
string= 456-123-852 123-8910
Example 2: Compressing Space Charactersdata one;   x='1 9 3 4 5 6 8';   y=compress(x,, 's');   put string=;run;Outputstring= 1934568
data one;   x='1 9 3 4 5 6 8';   y=compress(x,, 's');   put string=;run;
Output
string= 1934568
Example 3: Keeping Characters in the Listdata one;   x='Chemistry A Maths B Physics C';   y=compress(x, 'ABCD', 'k');   put string=;run;Outputstring= ABC
data one;   x='Chemistry A Maths B Physics C';   y=compress(x, 'ABCD', 'k');   put string=;run;
Output
string= ABC
Example 4: Compressing a String and Returning a Length of 0data _null_;   x='';   len=lengthn(compress(x));   put len=;run;Outputlen= 0
data _null_;   x='';   len=lengthn(compress(x));   put len=;run;
Output
len= 0
SAS Programming
Programming Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Data Types
Difference between Shallow and Deep copy of a class
Shallow Copy and Deep Copy in C++
Kotlin Array
Top 10 Programming Languages to Learn in 2022
Advantages and Disadvantages of OOP
Top 10 Fastest Programming Languages
Difference between while and do-while loop in C, C++, Java
Prolog | An Introduction
Difference between for and while loop in C, C++, Java