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

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
data_null__
Jade | Level 19
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

View solution in original post

12 REPLIES 12
LinusH
Tourmaline | Level 20
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
FreelanceReinh
Jade | Level 19

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: Dealing with Numeric Representation Error in SAS® Applications.

jmhorstman
Obsidian | Level 7

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.

 

 

FreelanceReinh
Jade | Level 19

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!).

 

 

jmhorstman
Obsidian | Level 7

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.  🙂

data_null__
Jade | Level 19
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
rogerjdeangelis
Barite | Level 11

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");
add.bigz(e1, e2);
));
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.

Rick_SAS
SAS Super FREQ

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.

Enio
Fluorite | Level 6
Superb, thanks!
Reeza
Super User
DS2 also has a bigint type.
Kurt_Bremser
Super User

@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.

Enio
Fluorite | Level 6
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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 12 replies
  • 2118 views
  • 14 likes
  • 9 in conversation