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

The Programming Expert

Solving All of Your Programming Headaches

  • 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.  SAS Percent Format – Formatting Number as Percent in SAS Dataset
  • 2.  SAS weekday function – Get Day of Week from Date Variable
  • 3.  SAS intnx – Add or Subtract Time from Date Variables in SAS Data Step
  • 4.  mod Function in SAS – Find Remainder of 2 Numbers After Division
  • 5.  SAS Comments – Commenting Your SAS Code the Right Way
  • 6.  SAS Not Equal – Check if a Variable is Not Equal to Another in Data Step
  • 7.  intcx SAS – Find Time Periods Between Two Dates in SAS Data Step
  • 8.  Round Number to 2 Decimal Places in SAS
  • 9.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 10.  SAS strip – Remove All Leading and Trailing Blanks from String Variable

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