SAS:将字符转换为数字变量 - 逗号作为小数点分隔符
SAS: Converting character to numeric variable - comma as a decimal separator
我正在尝试使用 INPUT 函数,因为它总是被建议使用,但似乎 SAS 在正确解释数量方面存在一些问题,例如:
2,30
1,61
0,00
...最后我得到了缺失值。可能是因为逗号是 SAS 来自的千位分隔符 ;)
data temp;
old = '1,61';
new = input(old, 5.2);
run;
为什么上面的结果是new = .?
我似乎找到了一些解决方法 - 在调用 INPUT 函数之前使用 TRANWRD 将逗号替换为句点(vide 下面的代码),但这是一个非常难看的解决方案,我想一定有一个合适的解决方案。
data temp;
old = '1,61';
new = input(tranwrd(old,',','.'), 5.2);
run;
您的示例中 new = .
的原因是因为 SAS 无法将逗号识别为小数点分隔符。请参阅日志中的注释。
NOTE: Invalid argument to function INPUT at line 4 column 11.
old=1,61 new=. ERROR=1 N=1
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to
missing values.
The documentation 包含各种 SAS 信息的列表。根据文档,您似乎可以使用 COMMAX
信息格式。
COMMAXw.d - Writes numeric values with a period that separates every three digits and a comma that separates the decimal fraction.
修改后的代码如下所示:
data temp;
old = '1,61';
new = input(old,commax5.);
run;
proc print;
结果输出为:
Obs old new
1 1,61 1.61
如果您想以相同的格式保留 new
变量,您只需将语句 format new commax5.;
添加到数据步骤即可。
感谢 Tom 指出 SAS 在 INPUT()
函数中使用信息格式。
我正在尝试使用 INPUT 函数,因为它总是被建议使用,但似乎 SAS 在正确解释数量方面存在一些问题,例如: 2,30 1,61 0,00 ...最后我得到了缺失值。可能是因为逗号是 SAS 来自的千位分隔符 ;)
data temp;
old = '1,61';
new = input(old, 5.2);
run;
为什么上面的结果是new = .?
我似乎找到了一些解决方法 - 在调用 INPUT 函数之前使用 TRANWRD 将逗号替换为句点(vide 下面的代码),但这是一个非常难看的解决方案,我想一定有一个合适的解决方案。
data temp;
old = '1,61';
new = input(tranwrd(old,',','.'), 5.2);
run;
您的示例中 new = .
的原因是因为 SAS 无法将逗号识别为小数点分隔符。请参阅日志中的注释。
NOTE: Invalid argument to function INPUT at line 4 column 11. old=1,61 new=. ERROR=1 N=1 NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.
The documentation 包含各种 SAS 信息的列表。根据文档,您似乎可以使用 COMMAX
信息格式。
COMMAXw.d - Writes numeric values with a period that separates every three digits and a comma that separates the decimal fraction.
修改后的代码如下所示:
data temp;
old = '1,61';
new = input(old,commax5.);
run;
proc print;
结果输出为:
Obs old new
1 1,61 1.61
如果您想以相同的格式保留 new
变量,您只需将语句 format new commax5.;
添加到数据步骤即可。
感谢 Tom 指出 SAS 在 INPUT()
函数中使用信息格式。