SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Hello,
I want to convert a character variable to numeric.
The character variable as shown in this example contains occurrences that are long.
After conversion, the numerical result obtained is not the same with that which is the one in the varaible character.
How can I get the same numbers in both formats?

Thank you

 

 

data char_to_num;
/*Conversion Charatere to numeric  */
x_char1 = "123456789123456789123456123456";
x_num1 = input(x_char1,best32.);
format x_num1 best32.2;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@mansour_ib_sas wrote:

Hello,
I want to convert a character variable to numeric.
The character variable as shown in this example contains occurrences that are long.
After conversion, the numerical result obtained is not the same with that which is the one in the varaible character.
How can I get the same numbers in both formats?

Thank you

 

 

data char_to_num;
/*Conversion Charatere to numeric  */
x_char1 = "123456789123456789123456123456";
x_num1 = input(x_char1,best32.);
format x_num1 best32.2;
run;

You cannot.

SAS stores numbers using 8 byte floating point values. You can only store about 15 digits of precision using that format.

What calculations you going to do with such a large number anyway that needs to preserve such a large level of precision?

If it is an identifier then keep it as a character string.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

@mansour_ib_sas wrote:

Hello,
I want to convert a character variable to numeric.
The character variable as shown in this example contains occurrences that are long.
After conversion, the numerical result obtained is not the same with that which is the one in the varaible character.
How can I get the same numbers in both formats?

Thank you

 

 

data char_to_num;
/*Conversion Charatere to numeric  */
x_char1 = "123456789123456789123456123456";
x_num1 = input(x_char1,best32.);
format x_num1 best32.2;
run;

You cannot.

SAS stores numbers using 8 byte floating point values. You can only store about 15 digits of precision using that format.

What calculations you going to do with such a large number anyway that needs to preserve such a large level of precision?

If it is an identifier then keep it as a character string.

mansour_ib_sas
Pyrite | Level 9

Thank you for Ansewer.

It's for If identifier. 

I did an itirative test, and I arrived at a precision level of 17

 

data char_to_num;
/*Conversion Charatere to numeric */
x_char1 = "123456789123456789123456123456";
x_num1 = input(x_char1,17.);
format x_num1 best32.2;
run;

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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
  • 2 replies
  • 2726 views
  • 1 like
  • 2 in conversation