• 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 / SAS tranwrd() Function – Replace Characters in Strings in SAS Data Step

SAS tranwrd() Function – Replace Characters in Strings in SAS Data Step

April 12, 2022 Leave a Comment

To replace all occurrences substrings in character variables when working in SAS, you can use the SAS tranwrd() function.

data k;
    a = 'this is a string with some characters and words';
    b = tranwrd(a,"t","mm");
    put b=;
run;

/* Output: */
b=mmhis is a smmring wimmh some characmmers and words

When working with character variables in SAS, the ability to manipulate and easily change the value of those variables is very important.

One such operation which is useful to be able to perform is the ability to replace given characters with other characters in a string.

The SAS tranwrd() function replaces all occurrences of a given string in a string variable.

tranwrd() takes 3 arguments. The first is the variable you want to make replacements in. The second argument is the substring to search for, and the third argument is the substring which will replace any found substrings.

The third argument must have a length greater than 1. If you pass a string with length 0 for the replacement, tranwrd() will use single spaces for the replacement.

Below are some examples of how you can use the SAS tranwrd() function to make replacements in character variables.

data k;
    a = 'this is a string with some characters and words';
    b = tranwrd(a,"t","mm");
    put b=;

    c = 'this is another string';
    d = tranwrd(c,"this","that");
    put d=;

    e = "Hello, my name is Jimmy.";
    f = tranwrd(e, ".","!");
    put f=;
run;

/* Output: */
b=mmhis is a smmring wimmh some characmmers and words
d=that is another string
f=Hello, my name is Jimmy!

Replacing Characters in String Variables with tranwrd() in SAS

There are a few more examples we can go over for how to use tranwrd() to make replacements in character variables in SAS.

For example, if you wanted to replace all underscores in a string with spaces, you could do that easily with the following SAS code.

data k;
    a = 'this_is_a_string';
    b = tranwrd(a,"_"," ");
    put b=;
run;

/* Output: */
b=this is a string

You can do this for other special characters too. For example, if you want to replace dashes with spaces, you can do that as shown below.

data k;
    a = 'this-is-a-string';
    b = tranwrd(a,"-"," ");
    put b=;
run;

/* Output: */
b=this is a string

Hopefully this article has been useful for you to learn how to use the SAS tranwrd() function.

Other Articles You'll Also Like:

  • 1.  Identifying Duplicates in SAS with PROC SORT dupout Option
  • 2.  SAS Not Equal – Check if a Variable is Not Equal to Another in Data Step
  • 3.  SAS right() Function – Right Align Character Variables in Data Step
  • 4.  SAS mean() Function – Find Average Across Columns in Data Step
  • 5.  SAS rand – Generate Random Numbers in a SAS Data Step
  • 6.  SAS year function – Get Year from Date Variable
  • 7.  SAS Uppercase – Make String Letters Uppercase with SAS upcase function
  • 8.  SAS case when – Conditional Logic with case Expression in PROC SQL
  • 9.  SAS substr() Function – Get Substring from Character Variable
  • 10.  SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)

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