# SAS Length Functions : LENGTH / LENGTHN / LENGTHC / LENGTHM.

**SAS Length Function**: LENGTH is a character function in SAS used to find the length of the character string. There are 4 variations available in SAS to find the length of a string. These are the LENGTH, LENGTHN, LENGTHC and LENGTHM. The primary objective of all the length functions is to return the length of the string however there are few variations in how these functions work and return the length.

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.

Below are the 4 Functions, that you may want to use depending on your requirements and scenarios.

Page Contents

## 1. 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 argument to the length function 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 no matter what the numeric value is**. 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 character, 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 and it would 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 |

## 2. LENGTHN Function

The LENGTHN function returns an integer that represents 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 also 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`

## 3. LENGTHC Function

The LENGTHC function returns an integer that represents 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 |

## 4. LENGTHM Function

The LENGTHM function returns an integer that represents the amount of memory in bytes that is 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 below example determines the amount of memory (in bytes) that is 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 |

Thank you Subhro! Really helpful.