问题
I have a big dataset from a data warehouse where one of the varibels have been read in as a string. The variable only contains numbers and I havn't got the raw data so I can't read in the data myself.
Example:
Variabel
4659721685425645
1234564578978896
7894567894567894
The format is $20. and the informat is $19.
I want to convert it into numeric so I can join the dataset with another dataset, on the same variable, but where this variable is numeric.
I have already tried:
data x_numeric;
set x;
format variable 20.;
run;
Grateful for help!
回答1:
A 20 digit number cannot be stored numerically in SAS with full precision. SAS uses double precision floating point numbers, meaning they store the number in 8 bytes. 52 bits are used for the mantissa (the precision), 11 for the exponent, and 1 for the sign. 2^53
is the largest number that could be stored precisely in SAS, or about 9x10^15 (any number of 15 digits, and most numbers of 16 digits). This is true on IEEE-compliant systems (Intel-based, so Unix, Windows, etc.); IBM mainframes use 56 bits for the mantissa, so up to 2^57 can be stored precisely.
See the SAS article on numeric precision for more details.
If your number can be safely represented numerically (ie, is less than 9E16), then you can convert it using input
.
var_n = input(var_c, 20.);
Where 20.
is the informat to read it in with (should be same width as the format
of the character variable, not its informat). You don't have to do this in the dataset permanently; if you're using sql join
you can just join using on tbl1.var = input(tbl2.var,20.)
or similar (though in a data step merge you couldn't do that).
来源:https://stackoverflow.com/questions/25139043/how-to-convert-text-to-numeric-format-in-sas