Hi, I have learned that the default length of a numeric variable is 8. However, when I use the length statement on a numeric variable, it returns 12.
data test; a = 1; len = length(a); run;
The result is 12:
Does anyone know why?
Thanks,
Sam
LENGTH is a character function. You are asking for the character length of a numeric variable. A read of the LENGTH documentation will tell you:
If string is a numeric constant, variable, or expression (either initialized or uninitialized), SAS automatically converts the numeric value to a right-justified character string by using the BEST12. format. In this case, LENGTH returns a value of 12 and writes a note in the SAS log stating that the numeric values have been converted to character values.
Important take away.... read the log.
LENGTH is a character function. You are asking for the character length of a numeric variable. A read of the LENGTH documentation will tell you:
If string is a numeric constant, variable, or expression (either initialized or uninitialized), SAS automatically converts the numeric value to a right-justified character string by using the BEST12. format. In this case, LENGTH returns a value of 12 and writes a note in the SAS log stating that the numeric values have been converted to character values.
Important take away.... read the log.
You are comparing apples and oranges.
The LENGTH() function returns the length of a value, not the storage length of the variable. Use the VLENGTH() function to find out the defined storage length for a variable. The reason you get 12 is because the LENGTH() function works on character strings, not numbers. SAS will convert the number to a character string using the BEST12 format. So no matter what numeric value you try to pass to the LENGTH() function the answer will always be 12.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.