Table of Contents
The TRANSLATE function is a useful tool in SAS that allows users to replace specific characters or words in a string with other characters or words. This function can be used in various scenarios, such as data cleaning, data manipulation, and data analysis.
To use the TRANSLATE function, users need to specify the input string, the characters to be replaced, and the replacement characters. The function will then search for the specified characters in the input string and replace them with the assigned replacement characters.
For example, if a dataset contains a column with country names written in different languages, the TRANSLATE function can be used to standardize the names into one language. Another example is when dealing with special characters in a dataset; the function can be used to replace them with regular characters for better data analysis.
In summary, the TRANSLATE function in SAS is a valuable tool for data manipulation and can be used in various scenarios to efficiently modify and clean data.
Use the TRANSLATE Function in SAS (With Examples)
You can use the TRANSLATE function in SAS to replace all occurrences of specific characters in a string with new characters.
This function uses the following syntax:
TRANSLATE(source, to, from)
where:
- source: Name of the variable to search
- to: Characters to use as replacement
- from: Characters to replace
The following examples show how to use this function in practice with the following dataset in SAS:
/*create dataset*/
data my_data;
input team $ position $ points assists;
datalines;
A Guard 14 4
A Guard 22 6
A Guard 24 9
A Forward 13 8
A Forward 13 9
A Guard 10 5
B Guard 24 4
B Guard 22 6
B Forward 34 2
B Forward 15 5
B Forward 23 5
B Guard 10 4
;
run;
/*view dataset*/
proc printdata=my_data;

Example 1: Use TRANSLATE to Replace Characters with New Characters
The following code shows how to use the TRANSLATE function to replace each occurrence of the letter “r” in the position column with a “z” instead:
/*create new dataset*/
data new_data;
set original_data;
position = translate(position, "z", "r");
run;
/*view new dataset*/
proc printdata=new_data;
Notice that each occurrence of the letter “r”” in the position column has been replaced with the letter “z” instead.
Example 2: Use TRANSLATE to Replace Characters with Blanks
The following code shows how to use the TRANSLATE function to replace each occurrence of “r” in the position column with a blank instead:
/*create new dataset*/
data new_data;
set my_data;
position = compress(translate(position, "", "r"));
run;
/*view new dataset*/
proc printdata=new_data;
This has the same effect as simply removing the string “r” from each position name.
Note #1: We wrapped the function around the TRANSLATE function to remove all blanks from the strings in the position column.
Note #2: You can find the complete documentation for the SAS TRANSLATE function .
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How can the TRANSLATE function be used in SAS, and what are some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-translate-function-be-used-in-sas-and-what-are-some-examples/
stats writer. "How can the TRANSLATE function be used in SAS, and what are some examples?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-translate-function-be-used-in-sas-and-what-are-some-examples/.
stats writer. "How can the TRANSLATE function be used in SAS, and what are some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-translate-function-be-used-in-sas-and-what-are-some-examples/.
stats writer (2024) 'How can the TRANSLATE function be used in SAS, and what are some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-translate-function-be-used-in-sas-and-what-are-some-examples/.
[1] stats writer, "How can the TRANSLATE function be used in SAS, and what are some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the TRANSLATE function be used in SAS, and what are some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
