SAS Functions

How to Easily Replace Characters in a String in SAS

In this article, we answer the question of how to replace characters in a SAS string?

How to Replace a Character in SAS

These are the steps to replace a character in a string in SAS

  1. Start the TRANWRD function.

    The TRANWRD function is a versatile function to replace one value with another.

  2. Specify the input variable that contains the character you want to replace.

    This input variable can be a column name, a string, or an expression.

  3. Specify the character that you want to replace.

    The character you want to replace must be written between quotes. Note that the TRANWRD function is case-sensitive.

  4. Specify the character that replaces the unwanted character.

    The character that replaces the unwanted character must be written between quotes.

  5. Close the TRANWRD function.

    Close the TRANWRD function with the closing parenthesis.

In the example below, we show how to use the TRANWRD function to replace a question mark with an exclamation mark.

data _null_;
    my_string = "Hello World?";
    my_new_string = tranwrd(my_string,"?", "!");
 
    put "My String: " my_string;
    put "My New String: " my_new_string;
run;

The image below shows the result of the code above.

Another common question is how to replace a blank with an underscore?

In SAS, you replace a blank with an underscore using the TRANWRD function. Firstly, you define the string that contains the blanks. Secondly, you write a blank space between quotes. Finally, you write an underscore between quotes. All three arguments must be separated by a comma,

In the example below, we show how to replace all blanks with an underscore.

How to Replace a Substring in SAS

Instead of replacing just one character in a string, the TRANWRD can also replace complete words or substrings.

These are the steps to replace a substring in SAS:

  1. Begin the TRANWRD function with an opening parenthisis.
  2. Specify the input variable that contains the substring you want to replace.
  3. Specify the the substring you want to replace, i.e. the target.
  4. Specify the replcement of the unwanted substring.
  5. End the TRANDWRD function with a closing parenthisis.

The input variable, the target, and the replacement can be variables (i.e. columns), strings written between quotes, or SAS expressions. Remember that all three arguments of the TRANWRD function are case-sensitive.

In the example below, we show how to replace the word “Street” with the word “Avenue“.

data work.ds;
    input address $1-20;
    datalines;
Lombard Street
Abbey Road
Fifth Avenue
Beale Street
DOWNING STREET
Bourbon Street
Kings Road
;
run;
 
data work.ds_tranwrd;
    set work.ds;
 
    address_tranwrd=tranwrd(address, 'Street', 'Avenue');
run;
Find and Replace in SAS with TRANWRD

Note that the fifth row remains unchanged. This is because the TRANWRD is case-sensitive.

How to Remove Characters in SAS

Instead of replacing characters, you could also use the TRANWRD function to remove characters. However, this is not the intended use of the TRANWRD function. For this reason, we recommend using the COMPRESS function to remove characters.

3 thoughts on “How to Easily Replace Characters in a String in SAS

Comments are closed.