Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » Base SAS » Length functions in SAS : LENGTH / LENGTHN / LENGTHC / LENGTHM.

Length functions in SAS : LENGTH / LENGTHN / LENGTHC / LENGTHM.

BySubhro Posted onOctober 24, 2020July 29, 2022 Last Updated:July 29, 2022
2 Comments

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 function is to return the length of the string however there are few variations in how these functions work and return the length.

SAS Length Function

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. SAS LENGTH Function
  • 2. LENGTHN Function
  • 3. LENGTHC Function
  • 4. LENGTHM Function

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!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #Character functions#length and lengthc in sas#length function in sas#lengthn sas#sas functions
Subhro

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.

Facebook Twitter Linkedin

Post navigation

Previous Previous
Count of missing and nonmissing values for each variable in a SAS data set
NextContinue
Box and Whisker Plot : Explained

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You