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

## Really basic question!

Hi,

I cannot get SAS to sum two long integers, I've tried numerous formats each to no avail. Problem is:

``````format x bestx64.;
x = sum(9014242673057788,11016614466661);``````

x should equal (I think) 9025259287524449, but SAS returns 9025259287524448

Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
Jade | Level 19

## Re: Really basic question!

``````49         proc ds2;
50            data _null_;
51               declare bigint x y z;
52               method run();
53                  x=9014242673057788;
54                  y=11016614466661;
55                  z=y+x;
56                  put _all_;
57                  end;
58               enddata;
59            run;
_N_=1 x=9014242673057788 y=11016614466661 z=9025259287524449``````
12 REPLIES 12
Tourmaline | Level 20

## Re: Really basic question!

I think taht the numbers are just to high. See SAS doc and numerical precission for you operating environment.
Is this a real life example?
Data never sleeps
Jade | Level 19

## Re: Really basic question!

Hi @Enio,

I think, LinusH is perfectly right. You're probably working on a Windows or Unix machine, aren't you? Please see the table "Largest Integer That Can Be Safely Stored in a Given Length" in "Numerical Accuracy in SAS Software". There you see that your first argument to the SUM function is slightly larger than the largest integer that can be "safely stored" in a numeric variable of maximum length (i.e. 8 bytes) on Windows/Unix systems. The result of the summation is, of course, even larger.

To understand what's happening and how SAS obtains 9025259287524448, read the document linked above. Here's another technical paper on the subject:

Obsidian | Level 7

## Re: Really basic question!

The first number in your SUM function is just a hair larger than the maximum 8-byte integer in SAS, which is 9,007,199,254,740,992.  There's a SAS support article called Numerical Accuracy in SAS Software that may be helpful.

However, here is one way to do it, although the result will be a character string, not actual numerical data.  It's admittedly a bit of a hack, but I'm basically breaking off the last 10 digits of each number, adding them separately, then adding the parts before the last 10 digits separately, and then concatenating the resulting sums as one long string:

data temp;
a1 = 901424;
a2 = 2673057788;
b1 = 1101;
b2 = 6614466661;
x1 = a1+b1;
x2 = a2+b2;
sum = cats(put(x1,10.),put(x2,10.));
run;

Note that a1 and a2 represent the two parts of your first number, b1 and b2 are the two part of your second number, and x1 and x2 are the two parts of the answer.  Choosing 10 digits was somewhat arbitrary, but it keeps you well under the maximum allowable integer.  Obviously, if your numbers got much bigger, you might have to split into more parts.

Edit to add: As noted below, this method only works if you break both numbers in a place where no carrying will occur, so it's really not an acceptable solution in general.

Jade | Level 19

## Re: Really basic question!

It should be noted that the "concatenation method" proposed by @jmhorstman does not generalize well:

Change only one digit in the first number

`9014243673057788`

and you get a nonsense result (off by a factor of almost 10!).

Obsidian | Level 7

## Re: Really basic question!

OK, yeah, good point @FreelanceReinh, because the carrying doesn't work out.  Guess I got lucky the first time.  Additional logic could be added to deal with that possibility, but it could get pretty ugly if the carrying cascades all the way left.  Like I said, it was a hack anyway.  🙂

Jade | Level 19

## Re: Really basic question!

``````49         proc ds2;
50            data _null_;
51               declare bigint x y z;
52               method run();
53                  x=9014242673057788;
54                  y=11016614466661;
55                  z=y+x;
56                  put _all_;
57                  end;
58               enddata;
59            run;
_N_=1 x=9014242673057788 y=11016614466661 z=9025259287524449``````
Barite | Level 11

## Re: Really basic question!

Hi Team

If you have IML you can call R or use my macro

%utl_submit_r64(%nrbquote(
library(gmp);
e1 <- as.bigz("1111111111111111111111111111111111111111");
e2 <- as.bigz("2222222222222222222222222222222222222222");
));
Big Integer ('bigz') :
[1] 3333333333333333333333333333333333333333

I think there are at least a dozen operators in the gmp package, in addition to add.

Python has this builtin?

R can read sas7bdats and output almost any format. I like to use MS-Access and stata because they are lossless and allow long variable names and char vars over 200bytes.

SAS Super FREQ

## Re: Really basic question!

Slightly off topic, but I once investigated extended-precision operations (by hand, not automatically) in SAS. See the article

"Computing 200 factorial: Using PROC IML as a really BIG calculator"

@data_null__ has the best solution (DS2) if you need big integers in a real work-related computation.

Fluorite | Level 6

Superb, thanks!
Super User

## Re: Really basic question!

DS2 also has a bigint type.
Super User

## Re: Really basic question!

@Reeza wrote:
DS2 also has a bigint type.

But the problem will arise again if the user tries to store those values in SAS datasets. One will probably have to store them as character and reconvert to bigint for further calculations. Or use a bigint-capable DBMS for storage.

Fluorite | Level 6

## Re: Really basic question!

Many thanks. You're quite right, there are some downstream sections to this process which will have to use bigint-capable DBMS as they are not able to utilise PROC DS2. Appreciate you taking the time to add this.
Discussion stats
• 12 replies
• 2111 views
• 14 likes
• 9 in conversation