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

How to display Numeric value more than 8 length except using BEST21. Format?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@Rakesh93 wrote:

How to display Numeric value more than 8 length except using BEST21. Format?


The length of a numeric variable is 8 per default, and cannot be larger. SAS stores numbers in 8 bytes in a real (floating-point) format.

The number of digits displayed can be set with a proper format. If you always want to see up to 21 integer digits (right-aligned), use the 21. format. Mind that, because of the way SAS stores numbers (see above), only 15-16 digits can be reliably stored. A higher precision is not possible with numbers in a SAS dataset.

See this code example:

data test;
input x1 32.;
format x1 21.;
datalines;
12345678901234567890
12345
123456789
;
run;

proc print data=test noobs;
run;

Result:

                  x1

12345678901234567168
               12345
           123456789

Note how the first number is changed because of the precision issue.

View solution in original post

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

Hi and welcome to the SAS Community 🙂

 

Can you provide an example?

Rakesh93
Fluorite | Level 6

For example:

data demo1;
input CRDNO ;
FORMAT CRDNO BEST21.;
CARDS;
123456789654123654789
987456321456987745669
RUN;
proc print; run;

 

If u run this program, the output will show same upto 16 numeric value but greater than 16th numeric value it shows different values. What should be done for getting same output as given in dataset ?

Kurt_Bremser
Super User

@Rakesh93 wrote:

For example:

data demo1;
input CRDNO ;
FORMAT CRDNO BEST21.;
CARDS;
123456789654123654789
987456321456987745669
RUN;
proc print; run;

 

If u run this program, the output will show same upto 16 numeric value but greater than 16th numeric value it shows different values. What should be done for getting same output as given in dataset ?


Store as character (read with $21.). If I translate "CRD" correctly as Central Registration Depository, then this is not a number, but an identification code.

Kurt_Bremser
Super User

@Rakesh93 wrote:

How to display Numeric value more than 8 length except using BEST21. Format?


The length of a numeric variable is 8 per default, and cannot be larger. SAS stores numbers in 8 bytes in a real (floating-point) format.

The number of digits displayed can be set with a proper format. If you always want to see up to 21 integer digits (right-aligned), use the 21. format. Mind that, because of the way SAS stores numbers (see above), only 15-16 digits can be reliably stored. A higher precision is not possible with numbers in a SAS dataset.

See this code example:

data test;
input x1 32.;
format x1 21.;
datalines;
12345678901234567890
12345
123456789
;
run;

proc print data=test noobs;
run;

Result:

                  x1

12345678901234567168
               12345
           123456789

Note how the first number is changed because of the precision issue.

hashman
Ammonite | Level 13

@Rakesh93:

You have to tell us what you mean by "numeric value more than 8 length". 

 

The system length of any numeric variable is always exactly 8 bytes. It's a collection of 8 character bytes SAS can interpret as a number using the double-float precision RB8. informat.

 

If you mean the "numeric length" as the numeric variable's value in terms of its constituent digits, it depends on what the numeric value is. If the value is an integer, SAS can store it in a numeric variable exactly if it is no greater than 2**53 under ASCII or 2**56 under EBCDIC, as the corresponding mantissa lengths under the two encoding systems are 53 and 56 bytes, respectively. If a number is greater, it is rounded. Hence, BEST21. will not display its digits exactly if it's greater than 2**53 or 2**56, correspondingly.

 

The exact character representation of a numeric SAS variable as it's stored, whether it's integer or fractional, is given by the RB8., HEX16., or BINARY64. formats, which give its representation as base 256, 16, or 2, respectively. To discern the actual mantissa and exponent from these representations, particularly when the number is signed, you have to delve into the knowledge of the details of its storage, such as the two's complement representation.

 

Kind regards

Paul D.  

 

     

Rakesh93
Fluorite | Level 6

For example:

data demo1;
input CRDNO ;
FORMAT CRDNO BEST21.;
CARDS;
123456789654123654789
987456321456987745669
RUN;
proc print; run;

 

If u run this program, the output will show same upto 16 numeric value but greater than 16th numeric value it shows different values. What should be done for getting same output as given in dataset ?

Tom
Super User Tom
Super User

@Rakesh93 wrote:

For example:

data demo1;
input CRDNO ;
FORMAT CRDNO BEST21.;
CARDS;
123456789654123654789
987456321456987745669
RUN;
proc print; run;

 

If u run this program, the output will show same upto 16 numeric value but greater than 16th numeric value it shows different values. What should be done for getting same output as given in dataset ?


Read those values as character strings.  You cannot store more than 15 digits exactly as a number.

data demo1;
  input CRDNO $21.;
CARDS;
123456789654123654789
987456321456987745669
;
hashman
Ammonite | Level 13

@Tom: "You cannot store more than 15 digits exactly as a number."

 

Unless you're using an EBCDIC platform, where the mantissa is 56 bits and so you can store an integer up to 72,057,594,037,927,936 accurately. I recall working for a credit card mainframe shop where visa 16-digit numbers were stored in a numeric variable.

Tom
Super User Tom
Super User

You don't need to know how many of the 64 bits are used for the mantissa versus the exponent.

Just ask SAS to tell you what is largest integer it can store without loss of digits.

 

Here is result on a machine using IEEE format for floating point numbers.

464   data _null_;
465    x= constant('exactint');
466    put x=comma32.;
467   run;

x=9,007,199,254,740,992

Note that credit card numbers can be stored since the first digit is never a 9.  https://money.howstuffworks.com/personal-finance/debt-management/credit-card1.htm

hashman
Ammonite | Level 13

@Tom:

"You don't need to know how many of the 64 bits are used for the mantissa versus the exponent.

Just ask SAS to tell you what is largest integer it can store without loss of digits."

 

It doesn't hurt to know. And if you do, you don't even have to ask SAS, as any calculator will tell you that 2**53=9007199254740992. 

 

"Note that credit card numbers can be stored since the first digit is never a 9."

 

On the mainframe, it doesn't matter since there, any 16-digit SAS number can be stored with full integer precision since with 56 bit in the mantissa 2**56=72057594037927936.  

 

Kind regards

Paul D.

ballardw
Super User

Since your variable name CRDNO makes me suspect that you are dealing with some sort of card or identification the question to ask yourself is: "Am I going to use this variable in numeric calculations?" If not, you are likely much better off with the value a character.

 

A second, possibly not so minor issue, is if any of your initial values have significant leading zeroes then making the variable numeric will remove the leading zeroes. And depending on your actual data may be a tad difficult to recover when needed.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 11 replies
  • 4269 views
  • 0 likes
  • 6 in conversation