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);
Any ideas?
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
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.
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.
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!).
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. 🙂
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
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.
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.
@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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.