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
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;
@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
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.
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;
Thanks alll!!!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.