Previous Page | | | Next Page |
Functions and CALL Routines
Returns a character string withspecified characters removed fromthe original string.
Category: | Character |
Restriction: | I18N Level 0 |
Tip: | DBCS equivalent function is KCOMPRESS in SAS National Language Support (NLS): Reference Guide. |
Syntax | |
Arguments | |
Details | |
Length of Returned Variable | |
The Basics | |
Examples | |
Example 1: Compressing Blanks | |
Example 2: Compressing Lowercase Letters | |
Example 3: Compressing Space Characters | |
Example 4: Keeping Characters in the List | |
Example 5: Compressing a String and Returning a Length of 0 | |
See Also |
Syntax |
COMPRESS(<source><, chars><,modifiers>)
Arguments
- source
specifies a character constant, variable,or expression from which specified characters will be removed.
- chars
specifies a character constant, variable,or expression that initializes a list of characters.
By default, the characters in this list are removed from the source argument. If you specify the Kmodifier in the thirdargument, then only the characters in this list are kept in the result.
Tip: You can add more charactersto this list by using other modifiers in the third argument. Tip: Enclose a literal stringof characters in quotation marks. - modifier
specifies a character constant, variable,or expression in which each non-blank character modifies the action of theCOMPRESS function. Blanks are ignored. The following characters can be usedas modifiers:
Tip: If the modifieris a constant, enclose it in quotation marks. Specify multiple constants ina single set of quotation marks. Modifier canalso be expressed as a variable or an expression.
Details |
Length of Returned Variable
In a DATAstep, if the COMPRESS function returns a value to a variable that has notpreviously been assigned a length, then that variable is given the lengthof the first argument.
The Basics
The COMPRESS function allowsnull arguments. A null argument is treated as a string that has a length ofzero.
Based on the number of arguments, the COMPRESS functionsworks as follows:
The COMPRESS function compiles a list of charactersto keep or remove, comprising the characters in the second argument plus anytypes of characters that are specified by the modifiers. For example, theD modifier specifies digits. Both of the following function calls remove digitsfrom the result:
COMPRESS(source, "1234567890");COMPRESS(source, , "d");
To remove digits and plus or minus signs, you can useeither of the following function calls:
COMPRESS(source, "1234567890+-");COMPRESS(source, "+-", "d");
Examples |
Example 1: Compressing Blanks
SAS Statements | Results |
---|---|
----+----1 | |
a='AB C D ';b=compress(a);put b; | ABCD |
Example 2: Compressing Lowercase Letters
SAS Statements | Results |
---|---|
----+----1----+----2----+----3 | |
x='123-4567-8901 B 234-5678-9012 c';y=compress(x,'ABCD','l');put y; | 123-4567-8901 234-5678-9012 |
Example 3: Compressing Space Characters
SAS Statements | Results |
---|---|
----+----1 | |
x='1 2 3 4 5';y=compress(x,,'s');put y; | 12345 |
Example 4: Keeping Characters in the List
SAS Statements | Results |
---|---|
----+----1 | |
x='Math A English B Physics A';y=compress(x,'ABCD','k');put y; | ABA |
Example 5: Compressing a String and Returning a Length of 0
SAS Statements | Results |
---|---|
----+----1 | |
x=' ';l=lengthn(compress(x));put l; | 0 |
See Also |
Previous Page | | | Next Page | | | Top of Page |
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.