*32*

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 print data=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 print data=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.

Note that the **LENGTH** function counts spaces in between words as characters, but it does not count any trailing blank spaces as characters.

If you would like to calculate the length of a character variable *including* trailing blanks, use the LENGTHC function instead.

**Additional Resources**

The following tutorials explain how to use other common functions in SAS:

How to Use the SUBSTR Function in SAS

How to Use the COMPRESS Function in SAS

How to Use the FIND Function in SAS

How to Use the COALESCE Function in SAS