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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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