SAS Compress: The Ultimate Guide to Shrink Data and Boost Speed
When SAS users search for "sas compress," they are usually chasing one of two goals: cleaning messy text with the COMPRESS function or shrinking large data sets with SAS data set compression. Both are valuable, but they solve different problems. The function works inside the DATA step and returns a transformed character string; the data set option changes how SAS stores an entire table on disk. Knowing which tool you need prevents wasted CPU, bloated libraries, and confusing syntax errors.
This guide walks through the practical side of SAS compression: how the COMPRESS function works, which modifiers matter, how COMPRESS=YES, CHAR, and BINARY differ, and when compression actually improves performance. The goal is not to memorize every option, but to make confident choices that keep your code clean and your SAS environment efficient.
What the SAS COMPRESS Function Actually Does
The COMPRESS function is a character-string utility. It scans a source string and removes characters you specify, or it can keep only the characters you specify when the K modifier is used. With no second argument, COMPRESS removes blanks, which is why COMPRESS("New York") returns "NewYork". That default behavior is handy for joining names or collapsing spaces, but it can surprise you if you expected spaces to remain.
The function does not change the original variable unless you assign the result back to it. In a DATA step, you might write clean_phone = COMPRESS(phone, , "kd"); to keep only digits. Or you might write clean_text = COMPRESS(text, , "ps"); to strip punctuation and spaces. Because COMPRESS returns a character value, it fits naturally into data cleansing, ID standardization, and text parsing routines.
Syntax, Modifiers, and Default Behavior
The syntax is COMPRESS(source, characters, modifiers). The source is the character value you want to process. The characters argument is a list of individual characters to remove. The modifiers argument changes the rule: A adds alphabetic characters, D adds digits, P adds punctuation, S adds spaces, and K flips the logic from remove to keep. The I modifier ignores case, which matters when you compare or filter letters.
A common mistake is forgetting the empty second argument when using only modifiers. COMPRESS(phone, "kd") tells SAS to remove the letters k and d, not to keep digits. The correct form is COMPRESS(phone, , "kd"). Small syntax details like that separate a quick clean-up from hours of debugging. Also remember that the default character list is blank, so COMPRESS(text) removes spaces but leaves punctuation, letters, and numbers untouched.
Practical Data Cleaning Examples
Suppose a customer file stores phone numbers as (555) 123-4567. To load a numeric-style key, use COMPRESS(phone, , "kd") to keep only digits. If you need to remove titles such as Mr., Mrs., or Dr. from a name field, COMPRESS can strip punctuation first, then TRIM or STRIP can handle remaining spacing. For postal codes, COMPRESS(zip, , "kd") removes hyphens and spaces from values like 12345-6789.
The function also helps when building concatenated keys. If first_name and last_name contain stray spaces, punctuation, or mixed case, you can create a key with COMPRESS(upcase(first_name || last_name), , "ka") to keep only alphabetic characters after uppercasing. That kind of standardization makes joins more reliable and reduces duplicate records caused by formatting differences.
SAS Data Set Compression for Storage and Performance
Data set compression is a different feature. It controls how SAS writes observations to disk. When you specify COMPRESS=YES, COMPRESS=CHAR, or COMPRESS=BINARY in a DATA statement, SAS stores the data in a compressed form and decompresses it when the data set is read. The result is often a smaller file, less disk I/O, and faster transfer across storage systems, though the CPU cost can rise.
You can set compression for a single data set, such as data want (compress=binary); set have; run;. You can also set a session-wide default with OPTIONS COMPRESS=YES; so new data sets inherit the behavior. The compression setting is applied at creation time, so changing an existing data set usually means recreating it with the desired option. Always check whether the trade-off matches your workload.
COMPRESS=YES, CHAR, and BINARY Compared
COMPRESS=YES is commonly treated as equivalent to character-oriented compression, often labeled CHAR. It works well when character variables contain repeated values, such as long category labels, blank-padded text, or recurring codes. It can be less effective on numeric-heavy tables where values vary widely, because the repeated-character pattern is weaker.
COMPRESS=BINARY uses a binary-oriented algorithm that can compress both character and numeric data. It is often the better first test for mixed tables, large fact tables, and data sets with many numeric columns or repeated numeric patterns. CHAR and BINARY both aim to reduce file size, but BINARY frequently wins on complex analytical data. The only reliable way to know is to test with your actual data and measure file size, CPU time, and elapsed time.
When Compression Helps and When It Backfires
Compression helps most when storage or I/O is the bottleneck. Large data sets with long character fields, repeated values, or many missing values often shrink dramatically. It also helps when you move data between environments, because smaller files take less time to copy and consume less network bandwidth. In those cases, the CPU spent compressing and decompressing can be a fair trade for faster input and output.
Compression backfires when CPU is the scarce resource. If a job already runs at high CPU utilization, adding compression can slow it down even if the file becomes smaller. Tiny data sets rarely benefit because the overhead outweighs the savings. Highly unique data, such as random identifiers or free-text fields with little repetition, may also compress poorly. Test with representative volumes before making compression a blanket policy.
A practical next step is to pick a few of your largest SAS data sets, create copies with COMPRESS=CHAR and COMPRESS=BINARY, and compare them using OPTIONS FULLSTIMER and available file-size reports. Keep the setting that improves storage without turning a fast job into a CPU-heavy one. With a little measurement, SAS compression becomes less of a guess and more of a deliberate performance choice.