BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ChickenLittle
Obsidian | Level 7

Hi,

 

I am trying to convert a character field to a numeric field without resulting in an exponent when the value is long.

 

Data:

obs    Field1

1          123456789012345

 

Code:

data Example;
set RawData;
Field2 = input(Field1, 16.);
run;

 

Result:

obs    Field2

1          1.23456E14

 

What I want as my result:

obs    Field2

1          123456789012345

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you input data with a 16. informat use a 16 wide FORMAT assigned to the value.

When you do not assign a format to a numeric value the default is BEST12. So stuffing 15 or 16 characters into 12 results in the Exponential display.

data Example;
Field2 = input('123456789012345', 16.);
format field2 best16.;
run;

 

Are these "numbers" actually a measurement or something that you will do arithmetic? If not leave them as character.

View solution in original post

4 REPLIES 4
ballardw
Super User

If you input data with a 16. informat use a 16 wide FORMAT assigned to the value.

When you do not assign a format to a numeric value the default is BEST12. So stuffing 15 or 16 characters into 12 results in the Exponential display.

data Example;
Field2 = input('123456789012345', 16.);
format field2 best16.;
run;

 

Are these "numbers" actually a measurement or something that you will do arithmetic? If not leave them as character.

ChickenLittle
Obsidian | Level 7

Thanks! And it is a key. But to spare me from modifying another original table, I'd rather just modify my table to match it.

ballardw
Super User

The format basically would have no impact on use such as joining a different table as I guess might be the use of something you call a key.

 

A somewhat silly example where one value is formatted as date to demonstrate that the format does not impact the join on the values.

data t1;
   input key value;
datalines;
12345  66
45612  99
;

data t2;
  input field1 othervalue;
  format field1 date9.;
datalines;
12345  99999
45612  88888
;

proc sql;
   create table example as
   select t1.*, t2.*
   from t1 as a
        left join
        t2 as b
   on t1.key=t2.field1
   ;
quit;
Tom
Super User Tom
Super User

You can store 16 digits as a number, but only up to the maximum integer that SAS can represent before it starts having to round values.  So unless you are positive the values will always be less than 9,007,199,254,740,992 you should keep it as character string.  There is no real reason to store identifiers as numbers since you won't be doing any arithmetic on them.

45   data _null_;
46     maxint=constant('exactint');
47     len = length(cats(maxint));
48     put len= maxint=:comma32.;
49   run;

len=16 maxint=9,007,199,254,740,992

 

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
  • 4 replies
  • 721 views
  • 3 likes
  • 3 in conversation