4.5 C
London
Thursday, December 19, 2024
HomeSASData Munging in SASHow to Use the COMPRESS Function in SAS (With Examples)

How to Use the COMPRESS Function in SAS (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the COMPRESS function in SAS to remove specific characters from a string.

This function uses the following basic syntax:

COMPRESS(String, characters to be removed)

where:

  • String: The string to analyze
  • characters to be removed: One or more specific characters to remove from string

Here are the four most common ways to use this function:

Method 1: Remove All Blank Spaces from String

data new_data;
    set original_data;
    compressed_string = compress(string_variable);
run;

Method 2:Remove Specific Characters from String

data new_data;
    set original_data;
    compressed_string = compress(string_variable, '!?@#');
run;

Method 3: Remove All Alphabetical Characters from String

data new_data;
    set original_data;
    compressed_string = compress(string_variable, '', 'a');
run;

Method 4: Remove All Numeric Values from String

data new_data;
    set original_data;
    compressed_string = compress(string_variable, '', 'd');
run;

The following examples show how to use each method with the following dataset in SAS:

/*create dataset*/
data original_data;
    input name $25.;
    datalines;
Andy Lincoln4 Bernard!
Barren Michael55 Smith!
Chad Simpson7 Arnolds?
Derrick Parson2 Henry
Eric Miller2 Johansen!
Frank Giovanni5 Goode
;
run;

/*view dataset*/
proc print data=original_data;

Example 1: Remove All Blank Spaces from String

The following code shows how to remove all blank spaces from each string in the name column:

/*remove blank spaces from each string in name column*/
data new_data;
    set original_data;
    compressed_name = compress(name);
run;

/*view results*/
proc print data=new_data;

Notice that all blank spaces have been removed from each string in the new column called compressed_name.

Example 2: Remove Specific Characters from String

The following code shows how to remove all question marks and exclamation points from each string in the name column:

/*remove question marks and exclamation points from each string in name column*/
data new_data;
    set original_data;
    compressed_name = compress(name, '?!');
run;

/*view results*/
proc print data=new_data;

Notice that all question marks and exclamation points have been removed from each string in the new column called compressed_name.

Example 3: Remove All Alphabetical Characters from String

The following code shows how to remove all alphabetical characters from each string in the name column:

/*remove all alphabetical characters from each string in name column*/
data new_data;
    set original_data;
    compressed_name = compress(name, '', 'a');
run;

/*view results*/
proc print data=new_data;

Notice that all all alphabetical characters have been removed from each string in the new column called compressed_name.

Example 4: Remove All Numeric Values from String

The following code shows how to remove all numeric values from each string in the name column:

/*remove all numeric values from each string in name column*/
data new_data;
    set original_data;
    compressed_name = compress(name, '', 'd');
run;

/*view results*/
proc print data=new_data;

Notice that all all numeric values have been removed from each string in the new column called compressed_name.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Use the SUBSTR Function in SAS
How to Use the FIND Function in SAS
How to Use the COALESCE Function in SAS

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories