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

Hi all SAS experts.

 

I have some questions about the length of a numeric variable as below:

1. Following this document, "The default length of numeric variables in SAS data sets is 8 bytes. The precision of this type of floating-point values is 16 decimal digits", I am not sure whether 8 bytes contains 16 numbers (like:1234567891234567) or just 8 numbers (like 12345678). (It seems to be the former case following this article but I still want to cross check)

 

2. Is there any maximum length for a numeric variable in SAS?

 

3. If we set up a length of 100 for a numeric variable while it only needs space for 10 digits, is there a big difference between the operating speed (says for example 1 million observations). I ask this question because I am wondering why people want to know the exact length of a variable then set the length rather than set a quite high length for a numeric variable. Or anything else I need to concern about apart from the operating speed?

 

Warm regards and thanks in advance!

 

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Computers work on binary. A byte is generally 8 bits of binary information (1 or 0 stored in memory) . So a single byte without consideration for sign theoretically stores a numeric value form 0 to 2**8. (actually 2**8-1 = 255, 0 to 255 is 256 integer values. )

So INTEGER storage looks like

Significant Digits and Largest Integer by Length for SAS Variables under Windows
Length in Bytes
Largest Integer Represented Exactly
Exponential Notation
Significant Digits Retained
3
8,192
213
3
4
2,097,152
221
6
5
536,870,912
229
8
6
137,438,953,472
237
11
7
35,184,372,088,832
245
13
8
9,007,199,254,740,992
253
15

 

However approximate values much larger or smaller can be stored.

This code will display the largest value your system uses:

data _null_;
   x=constant('big');
   put x;
run;

Which on my system returns: 1.797693E308 . Note the exponent.
The SIGNIFICANCE in the above chart is how many of the digits are "reliable". Yes you can create a value much larger or smaller than the precision of your computer.

 

Maximum length is 8 for numerics, per the definitions. The FORMAT to display the values can be up to 32 characters, look at the W.D format documentation. Values that won't fit the format will be shifted to some form of exponential format if too large or too small for the defined format.

 

3. Won't happen. SAS will only accept a length of 8 for "setting up" numeric variables. The length of character variables can have an impact on performance as there are more characters pushed around. There are options like COMPRESSED that reduce storage space but some operations on character value may require them to get expanded to their full length for some manipulation and the "uncompress" may take more time.

 

Once upon a time when computer memory was expensive limits for specific variables were often examined to save the cost. You may have heard of Y2K, the problems that a number of legacy systems had involving storing Year values as two characters? You are also talking a bit of cross purposes because "length" has a specific definition in SAS: number of bytes for storage, and is not "number of digits contained" and most computer languages are the same at heart.

If a variable is only every going to hold code values that are not manipulated in arithmetic then a 3-byte storage, allowing up to the 8,192 values listed above may make sense instead of storing "This the code that means the widget abcdef that is used for some specific purpose" in a data set. But the Length of numeric variables has an impact when doing arithmetic, especially that involves decimal values (percents for example). Generally not worth the work to handle properly.

 

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hi @Phil_NZ,

 

The length of 8 bytes for a numeric variable does not refer to the number of digits (as would be the case for a character variable), but to the number of bytes used to store the internal binary floating-point representation of a numeric value (which is very different from the familiar decimal representation, see Numerical Accuracy in SAS Software for details). Under Windows only lengths between 3 and 8 can be defined. Without good reasons you shouldn't use shorter lengths than the default 8. Let the Bug report: Incorrect results from PROC SQL subqueries working with shortened numerics be a warning to you.

ballardw
Super User

Computers work on binary. A byte is generally 8 bits of binary information (1 or 0 stored in memory) . So a single byte without consideration for sign theoretically stores a numeric value form 0 to 2**8. (actually 2**8-1 = 255, 0 to 255 is 256 integer values. )

So INTEGER storage looks like

Significant Digits and Largest Integer by Length for SAS Variables under Windows
Length in Bytes
Largest Integer Represented Exactly
Exponential Notation
Significant Digits Retained
3
8,192
213
3
4
2,097,152
221
6
5
536,870,912
229
8
6
137,438,953,472
237
11
7
35,184,372,088,832
245
13
8
9,007,199,254,740,992
253
15

 

However approximate values much larger or smaller can be stored.

This code will display the largest value your system uses:

data _null_;
   x=constant('big');
   put x;
run;

Which on my system returns: 1.797693E308 . Note the exponent.
The SIGNIFICANCE in the above chart is how many of the digits are "reliable". Yes you can create a value much larger or smaller than the precision of your computer.

 

Maximum length is 8 for numerics, per the definitions. The FORMAT to display the values can be up to 32 characters, look at the W.D format documentation. Values that won't fit the format will be shifted to some form of exponential format if too large or too small for the defined format.

 

3. Won't happen. SAS will only accept a length of 8 for "setting up" numeric variables. The length of character variables can have an impact on performance as there are more characters pushed around. There are options like COMPRESSED that reduce storage space but some operations on character value may require them to get expanded to their full length for some manipulation and the "uncompress" may take more time.

 

Once upon a time when computer memory was expensive limits for specific variables were often examined to save the cost. You may have heard of Y2K, the problems that a number of legacy systems had involving storing Year values as two characters? You are also talking a bit of cross purposes because "length" has a specific definition in SAS: number of bytes for storage, and is not "number of digits contained" and most computer languages are the same at heart.

If a variable is only every going to hold code values that are not manipulated in arithmetic then a 3-byte storage, allowing up to the 8,192 values listed above may make sense instead of storing "This the code that means the widget abcdef that is used for some specific purpose" in a data set. But the Length of numeric variables has an impact when doing arithmetic, especially that involves decimal values (percents for example). Generally not worth the work to handle properly.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 2 replies
  • 3320 views
  • 2 likes
  • 3 in conversation