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

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

问题描述

我一直在尝试使用 INPUT 函数,但是似乎SAS在正确解释金额方面存在一些问题,例如:
2,30
1,61
0,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.


注意:函数INPUT的无效参数在第4行第11列。
old = 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.

感谢汤姆指出SAS在 INPUT()中使用了信息格式功能。

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

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

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