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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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