How can I use the TRANWRD function in SAS and what are some examples of its usage? 2

How can I use the TRANWRD function in SAS and what are some examples of its usage?

The TRANWRD function in SAS is a powerful tool that allows users to easily manipulate and replace specific words or phrases within a string of text. This function can be used to make changes to data values, create new variables, or clean up inconsistent data. To use the TRANWRD function, the user must specify the string to be searched, the word or phrase to be replaced, and the new word or phrase to be inserted. This function also allows for the modification of multiple occurrences of a word or phrase within a string. Some examples of its usage include correcting misspelled words in a dataset, replacing abbreviations with full words, or modifying specific text in a report. Overall, the TRANWRD function provides a simple and efficient solution for data manipulation and cleaning tasks in SAS.

Use the TRANWRD Function in SAS (With Examples)


You can use the TRANWRD function in SAS to find and replace all occurrences of a specific pattern of characters in a string.

This function uses the following syntax:

TRANWRD(source, target, replacement)

where:

  • source: Name of the variable to search
  • target: Character pattern to search for
  • replacement: Character pattern to use for replacing target

The following examples show how to use this function in practice with the following dataset in SAS:

/*create dataset*/
data original_data;
    input team $1-20;
    datalines;
Fast Bees
Angry Hornets
Wild Mustangs
Fast Panthers
Fast Cobras
Wild Cheetahs
Wild Aardvarks
;
run;

/*view dataset*/
proc printdata=original_data;

Example 1: Use TRANWRD to Replace Characters with New Characters

The following code shows how to use the TRANWRD function to replace each occurrence of “Fast” in the team column with the string “Slow” instead:

/*create new dataset*/
data new_data;
    set original_data;
    team = tranwrd(team, "Fast", "Slow");
run;

/*view new dataset*/
proc printdata=new_data;

Notice that each occurrence of “Fast” in the team names have been replaced with “Slow” instead.

Example 2: Use TRANWRD to Replace Characters with Blanks

The following code shows how to use the TRANWRD function to replace each occurrence of “Fast” in the team column with a blank instead:

/*create new dataset*/
data new_data;
    set original_data;
    team = tranwrd(team, "Fast", "");
run;

/*view new dataset*/
proc printdata=new_data;

This has the same effect as simply removing the string “Fast” from each team name.

Note: You can find the complete documentation for the SAS TRANWRD function .

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). How can I use the TRANWRD function in SAS and what are some examples of its usage?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-tranwrd-function-in-sas-and-what-are-some-examples-of-its-usage/

stats writer. "How can I use the TRANWRD function in SAS and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-tranwrd-function-in-sas-and-what-are-some-examples-of-its-usage/.

stats writer. "How can I use the TRANWRD function in SAS and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-tranwrd-function-in-sas-and-what-are-some-examples-of-its-usage/.

stats writer (2024) 'How can I use the TRANWRD function in SAS and what are some examples of its usage?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-tranwrd-function-in-sas-and-what-are-some-examples-of-its-usage/.

[1] stats writer, "How can I use the TRANWRD function in SAS and what are some examples of its usage?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use the TRANWRD function in SAS and what are some examples of its usage?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top