BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kisumsam
Quartz | Level 8

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:

i1001.png

 

Does anyone know why?

 

Thanks,

Sam

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

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.

View solution in original post

3 REPLIES 3
RichardDeVen
Barite | Level 11

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.

kisumsam
Quartz | Level 8
Great thanks!
Tom
Super User Tom
Super User

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1166 views
  • 2 likes
  • 3 in conversation