4.2 C
London
Friday, December 20, 2024
HomeSASData Munging in SASHow to Remove Special Characters from Strings in SAS

How to Remove Special Characters from Strings in SAS

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...

The easiest way to remove special characters from a string in SAS is to use the COMPRESS function with the ‘kas’ modifier.

This function uses the following basic syntax:

data new_data;
    set original_data;
    remove_specials = compress(some_string, , 'kas');
run;

The following example shows how to use this syntax in practice.

Example: Remove Special Characters from String in SAS

Suppose we have the following dataset in SAS that contains the names of various employees and their total sales:

/*create dataset*/
data data1;
    input name $ sales;
    datalines;
Bob&%^ 45
M&$#@ike 50
Randy)) 39
Chad!? 14
Dan** 29
R[on] 44
;
run;

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

Notice that the values in the name column contain several special characters.

We can use the COMPRESS function to remove these special characters:

/*create second dataset with special characters removed from names*/
data data2;
  set data1;
  new_name=compress(name, , 'kas');
run;

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

Notice that the new_name column contains the values in the name column with the special characters removed.

Here’s exactly what the COMPRESS function did to remove these special characters:

  • k specifies that we would like to ‘keep’ certain characters
  • a specifies to keep alphabetic characters
  • s specifies to keep space characters

Note: You can find a complete list of modifiers for the COMPRESS function on this SAS documentation page.

Additional Resources

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

How to Extract Numbers from String in SAS
How to Use the SUBSTR Function in SAS
How to Convert Strings to Uppercase, Lowercase & Proper Case 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