Length functions in SAS : 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.

1. SAS 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

2. LENGTHN Function

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

3. LENGTHC Function

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

4. LENGTHM Function

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

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.