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

I've come across an inconsistent CHAR>NUM conversion issue.  I have numeric data stored in a character variable that I convert to numeric.  The issue is that while it works correctly for most of my data, there are multiple examples that do not convert correctly.  A simplified example is included below.  Interestingly, this code returns the same results on both Windows 10 and linux systems.  Any thoughts about what is happening would be most appreciated. 

 

Thanks,

Eric

=================================================================

Objective:  Converting number from a character variable to a numeric variable.

Specifics: 
- CH is a character variable
- Convert CH to the numeric variable NUM1 using approach 1 (add +0 to character variable; see code below)
- Convert CH to the numeric variable NUM2 using approach 2 (use the input function; see code below)

- Two additional approaches are used to generate NUM3 and NUM4 (see code below)
- Sometimes this works correctly (obs=1) and sometimes not (obs=2).  See results below.
- All four conversion approaches yield the same result (correct or not!).

Results/Listing:

Obs           ch                           num1                    num2
 1     3030614110316441        3030614110316441        3030614110316441
 2     9213200710116803        9213200710116804        9213200710116804

[NB:  num3 and num4 also are assigned the same values as num1 and num2.]

 

SAS Code:

data _x;                
 ch='3030614110316441';
num1=round(ch)+0;
num2=input(ch,best16.);
num3=ch*1;
num4=input(ch, ?? best32.);
output;

ch='9213200710116803';
num1=round(ch)+0;
num2=input(ch,best16.);
num3=ch*1;
num4=input(ch, ?? best32.);
output;

format num1-num4 20.;
run;
                     
proc print;             
run;                    

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Character strings that are 15 digits or more need to be kept as character. SAS cannot represent many fifteen digits number exactly, and it cannot represent any 16 digit or longer numbers exactly.

 

Keep these character strings as character.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Character strings that are 15 digits or more need to be kept as character. SAS cannot represent many fifteen digits number exactly, and it cannot represent any 16 digit or longer numbers exactly.

 

Keep these character strings as character.

--
Paige Miller
Eric87
Calcite | Level 5

Thank you.

 

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
  • 2 replies
  • 967 views
  • 0 likes
  • 2 in conversation