您将无法将如此大的整数精确地存储在 SAS 数字(64 位双精度数)中。
来自https://v8doc.sas.com/sashtml/win/numvar.htm
Windows 下 SAS 变量的有效数字和最大长度整数
| Length | Largest Integer | Exponential
| (Bytes) | Represented Exactly | Notation
+---------+-----------------------+------------
| 3 | 8,192 | 2 ^ 13
| 4 | 2,097,152 | 2 ^ 21
| 5 | 536,870,912 | 2 ^ 29
| 6 | 137,438,953,472 | 2 ^ 37
| 7 | 35,184,372,088,832 | 2 ^ 45
| 8 | 9,007,199,254,740,992 | 2 ^ 53
因此,虽然C 中的所有 16 位十六进制值都可以清楚地适合 64 位整数,但并非所有这些都可以清楚地适合 SAS 数字(那些 >2**53)。然而,64 位双精度是 IEEE 标准,可以处理高达 ~ 10**51 的数字,尽管会损失一些精度。
这段代码显示了两个不同的大整数赋值被存储为与赋值不同的相同 SAS 数值。
data _null_;
x = 837886600861902976;
y = 837886600861902977;
put x= 20. / y= 20.;
if x=y then put 'Huh? Ooohhh.';
run;
---------------------
x=837886600861902848
y=837886600861902848
Huh? Ooohhh.
一种方法是将 c-hex 字符串拆分为“上”和“下”部分,并使用它们的输入执行算术运算。需要注意的是,当实际的十六进制值 > 2**53 时,会损失清晰度/精度。
data _null_;
length H $30;
input H;
put h=;
if H =: '0x' then do;
HX = substr(H,3); * skip over 0x;
if (length (HX) > 15) then do;
HX_lower = substr(HX, length(HX)-14);
HX_upper = substr(HX, 1, length(HX)-15);
if length (HX_lower) > 15 then put 'ERROR: ' H= 'too big for two parts';
H_lower = input (HX_lower,hex15.);
H_upper = input (HX_upper,hex15.);
U = H_upper * 16**15;
L = H_lower;
put / H;
put HX_upper $16. ' ->' H_upper 20. ' ->' U 25.;
put HX_lower $16. ' ->' H_lower 20. ' ->' L 25.;
H_num = U + L;
end;
else do;
H_num = input (HX, hex15.);
end;
end;
else
put 'ERROR: ' H= 'is not c hex';
put H= H_num= comma30.;
datalines;
0x1
0x1b
0x1ba
0x1ba0
0x1ba0c
0x1ba0c5
0x1ba0c56
0x1ba0c56c
0x1ba0c56c9
0x1ba0c56c96
0x1ba0c56c966
0x1ba0c56c966e
0x1ba0c56c966ee
0x1ba0c56c966ee8
0x1ba0c56c966ee8b
0x1ba0c56c966ee8b0
0x1ba0c56c966ee8b00
0x1ba0c56c966ee8b000
0x1ba0c56c966ee8b0000
magic!
run;