Functions and CALL Routines: COMPRESS Function (2024)

Functions and CALL Routines: COMPRESS Function (1)

Functions and CALL Routines: COMPRESS Function (2)

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:

a or A

adds alphabetic characters to thelist of characters.

c or C

adds control characters to the listof characters.

d or D

adds digits to the list of characters.

f or F

adds the underscore character andEnglish letters to the list of characters.

g or G

adds graphic characters to the listof characters.

h or H

adds a horizontal tab to the list of characters.

i or I

ignores the case of the charactersto be kept or removed.

k or K

keeps the characters in the listinstead of removing them.

l or L

adds lowercase letters to the listof characters.

n or N

adds digits, the underscore character,and English letters to the list of characters.

o or O

processes the second and third argumentsonce rather than every time the COMPRESS function is called. Using the O modifierin the DATA step (excluding WHERE clauses), or in the SQL procedure, can makeCOMPRESS run much faster when you call it in a loop where the second and thirdarguments do not change.

p or P

adds punctuation marks to the listof characters.

s or S

adds space characters (blank, horizontaltab, vertical tab, carriage return, line feed, and form feed) to the listof characters.

t or T

trims trailing blanks from the firstand second arguments.

u or U

adds uppercase letters to the listof characters.

w or W

adds printable characters to thelist of characters.

x or X

adds hexadecimal characters to thelist of characters.

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:

Number of Arguments Result
only the first argument, source The argument has all blanks removed. If the argumentis completely blank, then the result is a string with a length of zero. Ifyou assign the result to a character variable with a fixed length, then thevalue of that variable will be padded with blanks to fill its defined length.
the first two arguments, sourceand chars All characters that appear in the second argument areremoved from the result.
three arguments, source, chars, andmodifier(s) The K modifier (specified in the third argument) determineswhether the characters in the second argument are kept or removed from theresult.

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

Functions:

COMPBL Function

LEFT Function

TRIM Function

Functions and CALL Routines: COMPRESS Function (3)

Functions and CALL Routines: COMPRESS Function (4)

Previous Page|Next Page|Top of Page

Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.

Functions and CALL Routines: COMPRESS Function (2024)

References

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 6158

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.