Are you a SAS enthusiast who gets confused by the different SAS length functions? You’re not alone. In this guide, we’ll explore LENGTH, LENGTHN, LENGTHC, and LENGTHM functions in detail and help you use them like a pro.

#### Why SAS Length Functions Matter

Need to find the length of a character string in SAS? The SAS length functions are your friends. Knowing which one to use can make SAS programming easier.

#### Meet the SAS Length Functions

The main purpose of length functions is to return the length of a string. However, there are variations in how they work and return the length.

**LENGTH**: The OG of SAS length functions. It returns the position of the rightmost non-blank character in a string.**LENGTHN**: Similar to LENGTH but with a twist. It returns 0 for a blank or missing string.**LENGTHC**: This one considers both blank and non-blank characters in the string.**LENGTHM**: The tech-savvy function that tells you the amount of memory allocated for the string.

Remember In SAS, the length of a variable is the number of bytes SAS allocates for storing the variable. It is not necessarily the same as the number of characters in the variable.

## Understanding the LENGTH Function

The LENGTH function returns the position of the rightmost non-blank character in a string excluding trailing blanks if any.

`result = LENGTH('SAS');`

In this example, the value of the result will be 3.

Now, what is the result of the length function if the argument is blank?

One might expect the LENGTH function for a NULL character string would return 0, but that is not correct.

**The length returned by Length () of a space(” “) or missing character value is always 1.**

So,

`result = LENGTH('');`

would return 1.

Another important point to remember for the LENGTH function is :

If a numeric value is passed to the LENGTH function, then SAS will convert it to a right-justified character string, using the BEST12. format.

In this case, the **LENGTH function will return the 12 regardless of the numeric value**.

It will also write a note to the SAS log, saying that numeric values have been converted to character values, which is something which should always be avoided – you should want to be in control of when numeric values are converted to characters, and not leave this to SAS.

Example –

`result = LENGTH(20);`

The above code will return 12 and writes the below note in SAS Log.

NOTE: Numeric values have been converted to character values at

the places given by: (Line):(Column). 75:18

**Bonus Tip:**

Now, the question is, if the length function always returns 12 for a numeric value, how would you find the length of a numeric value? In this case, the length means the number of digits.

Here is a simple trick to find the length of a numeric variable in SAS. Use the below code to return the number of digits in the variable, assuming there are no decimals.

`len = int(log10(result))+1;`

LENGTH Function returns the length of the string, excluding any trailing blanks.

```
data _null_;
l1=length('This statement has trailing blanks ');
l2=length('');
put l1= l2=;
run;
```

**Output:**

l1=34 l2=1

To conclude:

**Syntax**: LENGTH(‘Your String’)**Example**: result = LENGTH(‘SAS’); (Returns 3)**Pro Tip**: LENGTH always returns 1 for a NULL character string.

## How LENGTHN Differs from LENGTH

The LENGTHN function returns an integer representing the position of the rightmost non-blank character in a string.

**If the value of the string is blank or missing, LENGTHN returns a value of 0.**

So, unlike the LENGTH function, the below code would return 0.

`result = LENGTHN('');`

If the string is a numeric variable, LENGTHN returns a value of 12 and prints a note in the SAS log that the numeric values have been converted to character values.

LENGTHN Function returns the length of the string, excluding any trailing blanks.

```
data _null_;
ln1=lengthn('This statement has trailing blanks ');
ln2=lengthn('');
put ln1= ln2=;
run;
```

**Output:**

ln1=34 ln2=0

**LENGTHN: The Underdog**

**Syntax**: LENGTHN(‘Your String’)**Example**: result = LENGTHN(”); (Returns 0)**Did You Know?**: Unlike LENGTH, LENGTHN returns 0 for a blank or missing string.

## LENGTHC: What You Need to Know

The LENGTHC function returns an integer representing the position of the rightmost blank or non-blank character in the string.

If the value of the string is missing and contains blanks, LENGTHC returns the number of blanks in the string.

**If the value of the string is missing and contains no blanks, LENGTHC returns a value of 0.**

If the string is a numeric variable, LENGTHC returns a value of 12 and prints a note in the SAS log that the numeric values have been converted to character values.

```
data _null_;
x=lengthc('This statement has trailing blanks ');
lc=lengthc(x);
put x= lc=;
run;
```

**Output:**

lc1=37 lc2=1

## LENGTHM: Memory Allocation Explained

The LENGTHM function returns an integer representing the amount of memory in bytes allocated for the string.

If the string is a numeric variable, LENGTHM returns a value of 12 and prints a note in the SAS log that the numeric values have been converted to character values.

The example below determines the amount of memory (in bytes) allocated for a buffer that stores intermediate results in a character expression.

Since SAS does not know how long the value of the expression CAT(x, y) is, it allocates memory for values up to 32,767 bytes long.

```
data _null_;
x='SAS';
y='y';
lc=lengthc(cat(x, y));
lm=lengthm(cat(x, y));
put lc= lm=;
run;
```

**Output:**

lc=4 lm=32767

**Quick Tips for LENGTHC and LENGTHM**

**LENGTHC**: Returns the number of blanks in a missing string.**LENGTHM**: Tells you the memory allocation in bytes.

#### Wrapping Up

Here is a comprehensive guide to mastering SAS Length Functions. Keep in mind that selecting the appropriate function can greatly impact your SAS projects.

**Ready for more SAS tips and tricks?** Stay tuned for our upcoming guides!

Thank you Subhro! Really helpful.

Thank you Benhaz