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;
@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 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.
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;
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.