Table of Contents
To replace characters in a string in SAS, one can use the “TRANSLATE” function. This function allows the user to specify the characters to be replaced and the characters to replace them with. For example, if one wants to replace all instances of “a” with “b” in a string, the syntax would be: TRANSLATE(string, ‘a’, ‘b’). Additionally, the “TRANSLATE” function can also be used to remove characters by replacing them with a blank space. Overall, the “TRANSLATE” function provides a simple and efficient way to manipulate strings in SAS.
Replace Characters in a String in SAS (With Examples)
You can use the tranwrd() function to replace characters in a string in SAS.
Here are the two most common ways to use this function:
Method 1: Replace Characters in String with New Characters
data new_data;
set original_data;
new_variable = tranwrd(old_variable, "OldString", "NewString");
run;
Method 2: Replace Characters in String with Blanks
data new_data;
set original_data;
new_variable = tranwrd(old_variable, "OldString", "");
run;The following examples show how to use each method with the following dataset in SAS:
/*create dataset*/
data original_data;
input team $1-20;
datalines;
Angry Bees
Angry Hornets
Wild Mustangs
Kind Panthers
Kind Cobras
Wild Cheetahs
Wild Aardvarks
;
run;
/*view dataset*/
proc printdata=original_data;
Example 1: Replace Characters in String with New Characters
The following code shows how to replace the word “Wild” in the team variable with the word “Fast”:
/*replace "Wild" with "Fast" in team variable*/
data new_data;
set original_data;
new_team = tranwrd(team, "Wild", "Fast");
run;
/*view new dataset*/
proc printdata=new_data;
Notice that each team that had “Wild” in the name now has the word “Fast” in the name instead.
Any team that did not have “Wild” in the name simply kept their original name.
Example 2: Replace Characters in String with Blanks
The following code shows how to replace the word “Wild” in the team variable with a blank:
/*replace "Wild" with a blank in team variable*/
data new_data;
set original_data;
new_team = tranwrd(team, "Wild", "");
run;
/*view new dataset*/
proc printdata=new_data;
Notice that any team that had “Wild” in the name simply had the word “Wild” replaced with a blank.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How can I replace characters in a string in SAS? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-replace-characters-in-a-string-in-sas-can-you-provide-some-examples/
stats writer. "How can I replace characters in a string in SAS? Can you provide some examples?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-replace-characters-in-a-string-in-sas-can-you-provide-some-examples/.
stats writer. "How can I replace characters in a string in SAS? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-replace-characters-in-a-string-in-sas-can-you-provide-some-examples/.
stats writer (2024) 'How can I replace characters in a string in SAS? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-replace-characters-in-a-string-in-sas-can-you-provide-some-examples/.
[1] stats writer, "How can I replace characters in a string in SAS? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I replace characters in a string in SAS? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
