用 COMMAw,d 转换数字 [英] Converting numerics with COMMAw,d

查看:19
本文介绍了用 COMMAw,d 转换数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 SAS 帮助页面,并试图获得与页面底部示例中相同的结果.我的代码:

I am following the SAS help page and was trying to achieve the same results as in the examples to the bottom of the page. My code:

data _null_;
    test=23451.23;
    result=input(test,comma10.2);
    put 'this should be:' result;
run; 

在日志中输出

this should be:23451

而它应该是 23,451.23.没有错误、有用的注释或警告.

while it should be 23,451.23. There are no errors nor helpful notes nor warnings.

当我不使用输入函数时,它会提供正确的结果

When I am not using the input function, it delivers the correct result

data _null_;
        test=23451.23;
        format test comma10.2;
        put 'this should be:' test;
run;

这里发生了什么?不能结合输入COMMAw,d吗?

What is happening here? Is it not possible to combine input and COMMAw,d?

推荐答案

格式用于将值转换为字符串.Informats 用于将字符串转换为值.您可以将格式与 PUT 和 FORMAT 语句或 PUT() 函数一起使用.您可以将信息与 INPUT 和 INFORMAT 语句或 INPUT() 函数一起使用.

Formats are used to convert values into strings. Informats are used to convert strings into values. You use formats with PUT and FORMAT statements or PUT() function. You use informats with INPUT and INFORMAT statements or INPUT() function.

所以 INPUT() 函数需要一个字符串作为第一个参数,但你已经给了它一个数字.请注意,SAS 会在日志中放置一个注释,说明它必须将数字转换为字符. SAS 将使用 BEST12. 格式来转换您的数字,23451.23,变成12个字符的字符串' 23451.23'.然后,当 INPUT() 函数使用 COMMA10.2 信息时,它只读取前 10 个字符,而您错过了十进制数字.请注意,不仅宽度应该更长,而且您不应该在信息中包含小数点后的值.如果您的信息具有更小的宽度,您会错过小数点,而 SAS 会隐含一个(将整数值除以 100).

So the INPUT() function needs a character string as the first argument, but you have given it a number. Notice that SAS will put a NOTE in the log saying that it had to convert numbers to characters. SAS will use the BEST12. format to convert so your number, 23451.23, becomes the 12 character string ' 23451.23'. Then when the INPUT() function uses the COMMA10.2 informat it reads only the first 10 characters and you miss the decimal digits. Note that not only should the width be longer but you should NOT have included a value after the decimal point in the informat. If your informat had an even smaller width you would have missed the decimal point and SAS would have implied one (divided the integer value by 100).

如果您希望您的数字以特定方式显示,只需将所需的格式附加到变量即可.您可以将格式规范添加到 PUT 语句中.

If you want your numbers to be displayed in a particular way then just attach the desired format to the variable. You could just add the format specification to the PUT statement.

put test= comma10.2 ;

或者使用 FORMAT 语句将格式附加到变量.

Or attach the format to the variable using the FORMAT statement.

format test comma10.2;

如果要将数字转换为字符串,请使用PUT() 函数

If you want to convert your number into a character string then use the PUT() function

char_result = put(test,comma10.2);

PUTN() 函数.

char_result = putn(test,'comma10.2');

这篇关于用 COMMAw,d 转换数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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