SAS:将字符转换为数字变量 - 逗号作为小数分隔符 [英] SAS: Converting character to numeric variable - comma as a decimal separator

查看:49
本文介绍了SAS:将字符转换为数字变量 - 逗号作为小数分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 INPUT 函数,因为它总是被建议的,但似乎 SAS 在正确解释金额方面存在一些问题,例如:2,301,610,00...我最终得到了缺失值.也许这是因为逗号是 SAS 来自的千位分隔符;)

I'm trying to use INPUT function, as it is always suggested, but it seems that SAS has some problems with proper interpretation of amounts like: 2,30 1,61 0,00 ...and I end up with missing values. Perhaps it's caused by comma being thousands separator where SAS come from ;)

data temp;
    old = '1,61';
    new = input(old, 5.2);
run;

为什么上面的结果是new = .?

Why the result of above is new = .?

似乎我找到了一些解决方法 - 在调用 INPUT 函数之前使用 TRANWRD 将逗号替换为句点(vide 代码如下),但它是相当丑陋的解决方案,我想一定有一个合适的解决方案.

It seems that I've found some work-around - by replacing comma with a period using TRANWRD before INPUT function is called (vide code below), but it's quite ugly solution and I suppose there must be a proper one.

data temp;
    old = '1,61';
    new = input(tranwrd(old,',','.'), 5.2);
run;

推荐答案

您的示例中 new = . 的原因是因为 SAS 无法将逗号识别为小数分隔符.请参阅日志中的注释.

The reason new = . in your example is because SAS does not recognize the comma as a decimal separator. See the note in the log.

注意:第 4 行第 11 列的函数 INPUT 的参数无效.旧=1,61 新=.错误=1 N=1注意:以下地方不能进行数学运算.操作结果已设置为缺失值.

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.

文档 包含各种 SAS 信息的列表.根据文档,您似乎可以使用 COMMAX 信息.

The documentation contains a list of various SAS informats. Based on the documentation it looks like you can use the COMMAX informat.

COMMAXw.d - 写入数值,每三位之间用句点隔开,小数部分用逗号隔开.

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.; 添加到数据步骤即可.

If you want to keep the new variable in the same format you can just add the statement format new commax5.; to the data step.

感谢 Tom 指出 SAS 在 INPUT() 函数中使用信息.

Thanks to Tom for pointing out that SAS uses informats in the INPUT() function.

这篇关于SAS:将字符转换为数字变量 - 逗号作为小数分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆