sas - 在 SAS 中将十六进制字符(超过 16 个字节)转换为十进制数
问题描述
嗨,我有一个名为 Hexchar 的 SAS 字符列包含十六进制字符“0x1ba0c56c966ee8b0000”这实际上是十进制数的 8154350000000000000000。我想将“0x1ba0c56c966ee8b0000”转换为 8154350000000000000000。如果十六进制小于 16 个字节,则以下函数可以完成工作。
DexNumber=input(substr(Hexchar,3), hex16.); /* The reason why substr() function is to skip "0x" in the front */
然而,十六进制格式仅限于 16 个字节,不能处理更多字节。你知道任何转换这个的好逻辑吗?
解决方案
您将无法将如此大的整数精确地存储在 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
因此,虽然所有 16 位十六进制值C
都可以清楚地适合 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;
推荐阅读
- excel - 添加新工作表时,VBA Excel自动增加公式中的单元格编号
- spring - 扩展 JdbcCursorItemReader 时出现 SQL 查询错误
- sql - Azure Runbook 中未找到数据源名称且未指定默认驱动程序
- node.js - mongoose 命名空间没有导出成员“OptionalId”
- hdfs - sqoop - 数据导入期间作业失败
- linux - tail: 无法打开 ''$'\033''[35m/home/test/yr9.log'$'\033''[39m' 供阅读:没有这样的文件或目录
- blazor-server-side - 集成测试 Blazor 应用
- c++ - C ++:抽象联合和新放置运算符的行为?
- regex - 使用 yara 正则表达式检测端口号
- node.js - 如何使用 mongoose 从 mongodb 匹配日期获取日期