Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Really basic question!

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 04-14-2016 12:07 PM
(2117 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

```
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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?

Is this a real life example?

Data never sleeps

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

```
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
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Superb, thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

DS2 also has a bigint type.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

**Available on demand!**

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

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.