- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you.