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

Hi, i have a dataset where the numbers are stored as string and i want to perform some calculations to the numbers and convert it back into string. The result i got is wrong.

 

data b;
x='9458669011700000';
y=input(x,best32.) + 7783;
z=put(y,16.);

put y= best32.;
keep x y z;
run;

 

Current Result

z = 9458669011707784

 

Expected result

Z =9458669011707783

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@1477979 

As others already explained SAS datasets store numeric variables with 8 bytes and though you loose precision with 16 digits or more.

However SAS DS2 (Proc DS2) and Proc FedSQL allow for using other datatypes with higher precision. You just need to convert the result back to a string for storing the result with full precision in a SAS table.

 

Below a sample using Proc FedSQL.


data have;
  x='9458669011700000';
  output;
  stop;
run;

proc datasets lib=work nolist nowarn;
  delete want;
  run;
quit;
proc fedsql;
  create table want as
  select x, cast(cast(x as bigint) + 7783 as char(16)) as z
  from have
  ;
quit;

proc print data=want;
run;

Capture.JPG 

View solution in original post

4 REPLIES 4
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

@1477979  have a rounding issue where using best32. may not be the best format for your large numbers.

This link may have a format the fits your needs.

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001263753.htm

 

 

 

SASKiwi
PROC Star

SAS stores numbers in 8 bytes which is sufficient for 15 digits to be accurate. 16 digits can't be held accurately. You could consider splitting your number into 2 components, add the number then put the components back together.

Patrick
Opal | Level 21

@1477979 

As others already explained SAS datasets store numeric variables with 8 bytes and though you loose precision with 16 digits or more.

However SAS DS2 (Proc DS2) and Proc FedSQL allow for using other datatypes with higher precision. You just need to convert the result back to a string for storing the result with full precision in a SAS table.

 

Below a sample using Proc FedSQL.


data have;
  x='9458669011700000';
  output;
  stop;
run;

proc datasets lib=work nolist nowarn;
  delete want;
  run;
quit;
proc fedsql;
  create table want as
  select x, cast(cast(x as bigint) + 7783 as char(16)) as z
  from have
  ;
quit;

proc print data=want;
run;

Capture.JPG 

1477979
Calcite | Level 5

Thanks alll!!! 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 4480 views
  • 4 likes
  • 4 in conversation