• 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
  • VBA
  • 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 weekday function – Get Day of Week from Date Variable
  • 2.  SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
  • 3.  SAS tranwrd() Function – Replace Characters in Strings in SAS Data Step
  • 4.  SAS if then else – Write Conditional Expressions for Multiple Conditions
  • 5.  SAS left() Function – Left Align Character Variables in Data Step
  • 6.  SAS round – Rounding Numbers in a SAS Data Step
  • 7.  SAS contains() – Check if Variable Contains a String in Where Statement
  • 8.  SAS data _null_ – Create a SAS Dataset with No Records and Variables
  • 9.  Get Substring from Right in SAS
  • 10.  SAS scan Function – Return nth Word from Character String

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy