Table of Contents
The LENGTH function in SAS is a useful tool for determining the length of a character variable or string of characters. It can be utilized in various ways, such as calculating the length of a variable, truncating strings to a specific length, or comparing the length of different variables. This function is particularly useful for data manipulation and data cleaning tasks, as it allows for the efficient handling of character data. Some examples of its usage include determining the length of a person’s name, limiting the number of characters in a product description, or identifying any discrepancies in the length of data fields. In summary, the LENGTH function plays a crucial role in character data processing and is an essential function in SAS programming.
Use the LENGTH Function in SAS (With Examples)
You can use the LENGTH function in SAS to calculate the length of character variables, excluding trailing blanks.
This function uses the following basic syntax:
LENGTH(expression)
where:
- expression: The character string to analyze
The following example shows how to use this function in practice.
Example: Using the LENGTH Function in SAS
Suppose we have the following dataset in SAS:
/*create dataset*/
data original_data;
input team $1-21 points;
datalines;
Golden State Warriors 99
Brooklyn Nets 101
Utah Jazz 105
Cleveland Cavs 100
Atlanta Hawks 109
Milwaukee Bucks 98
Miami Heat 93
Houston Rockets 100
Los Angeles Lakers 112
;
run;
/*view dataset*/
proc printdata=original_data;
We can use the LENGTH function to calculate the length of each string in the team column:
/*calculate length of each string in team column*/
data new_data;
set original_data;
team_length = length(team);
run;
/*view results*/
proc printdata=new_data;

The new column called team_length displays the length of each string in the team column.
For example:
- The string “Golden State Warriors” has a length of 21.
- The string “Brooklyn Nets” has a length of 13.
- The string “Utah Jazz” has a length of 9.
- The string Cleveland Cavs” has a length of 14.
And so on.
If you would like to calculate the length of a character variable including trailing blanks, use the function instead.
The following tutorials explain how to use other common functions in SAS:
Cite this article
stats writer (2024). How can the LENGTH function be utilized in SAS and what are some examples of its usage?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-length-function-be-utilized-in-sas-and-what-are-some-examples-of-its-usage/
stats writer. "How can the LENGTH function be utilized in SAS and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-length-function-be-utilized-in-sas-and-what-are-some-examples-of-its-usage/.
stats writer. "How can the LENGTH function be utilized in SAS and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-length-function-be-utilized-in-sas-and-what-are-some-examples-of-its-usage/.
stats writer (2024) 'How can the LENGTH function be utilized in SAS and what are some examples of its usage?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-length-function-be-utilized-in-sas-and-what-are-some-examples-of-its-usage/.
[1] stats writer, "How can the LENGTH function be utilized in SAS and what are some examples of its usage?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the LENGTH function be utilized in SAS and what are some examples of its usage?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
