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

Showing results for

Find a Community

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-14-2016 12:07 PM

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?

Accepted Solutions

Solution

04-15-2016
06:28 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Enio

04-14-2016 03:05 PM

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

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Enio

04-14-2016 12:14 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Enio

04-14-2016 01:27 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Enio

04-14-2016 01:28 PM - edited 04-14-2016 03:41 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jmhorstman

04-14-2016 01:40 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to FreelanceReinhard

04-14-2016 01:42 PM

OK, yeah, good point @FreelanceReinhard, 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.

Solution

04-15-2016
06:28 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Enio

04-14-2016 03:05 PM

```
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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to data_null__

04-14-2016 04:35 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to data_null__

04-15-2016 06:11 AM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to data_null__

04-15-2016 06:28 AM

Superb, thanks!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Enio

04-14-2016 08:12 PM

DS2 also has a bigint type.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

04-15-2016 02:12 AM

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.

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

04-15-2016 06:31 AM

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.