• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / SAS / Remove Specific Character from String in SAS

Remove Specific Character from String in SAS

April 8, 2022 Leave a Comment

When working with strings in SAS, you can remove specific characters from a string with the SAS compress() function.

For example, if we wanted to remove the letters “a” and “b” from a string, we could do so with the following SAS code.

data k;
    a = 'Alfred and Betty went to the beach to play with a ball. ';
    b = compress(a,"abAB");
    put b=;
run;

/* Output: */
b=lfred nd etty went to the ech to ply with  ll.

When working with string variables in our SAS programs, the ability to be able to easily manipulate and change string variables is valuable.

One such case is when we want to remove specific characters from a string.

The SAS compress() function allows us to remove characters from strings easily.

compress() takes 3 arguments. The first argument is a character variable. The second argument is the characters you want to keep or remove – we can keep characters with compress() as well. For the third argument, you can add modifiers.

Below is an example of how you can remove specific characters from a string using compress() in SAS. In the example below, we are remove all “a”, “b”, “A”, and “B” characters.

data k;
    a = 'Alfred and Betty went to the beach to play with a ball. ';
    b = compress(a,"abAB");
    put b=;
run;

/* Output: */
b=lfred nd etty went to the ech to ply with  ll.

Removing Special Characters from a String in SAS with compress() Function

A common case where you might need to remove characters from a string variable is when you need to remove special characters from a string.

Special characters include punctuation, quotes, parentheses, etc.

We can remove special characters from a string variable in SAS with the help of the compress() function modifiers.

We can pass “kas” as a modifier which keeps (k) the alphabetical characters (a) and spaces (s) of a string variable.

data k;
    a = 'This is a -- string with some ^ ?,. special ## characters.';
    b = compress(a,,"kas");
    put b=;
run;

/* Output: */
b=This is a  string with some   special  characters

Hopefully this article has been useful for you to learn how to remove characters from strings in SAS.

Other Articles You'll Also Like:

  • 1.  Do Loop in SAS Macro Language
  • 2.  SAS mean() Function – Find Average Across Columns in Data Step
  • 3.  SAS include – Execute Code from Other Files in SAS with %include
  • 4.  SAS Remove Labels from Dataset with PROC DATASETS
  • 5.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 6.  Concatenating Strings in a SAS Data Step
  • 7.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 8.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 9.  IN in SAS – Checking if Variable is in Array of Values
  • 10.  catx SAS – Concatenate String Variables with Delimiter in SAS Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy