字符值除以数字(Character value dividing number)

我希望在两种情况下除法运算都会失败,因为操作不在数值之间。 第一个案例有效。 第二种情况无法按预期执行。 有人可以解释一下这个区别吗? 我使用的是SAS 9.04。

案例1(工作):

data work.products; Product_Number = 11; Item = '12'; Item_Reference = Item/Product_Number; run;

案例2(不工作):

data work.products; Product_Number = 11; Item = '12'; Item_Reference = Product_Number/Item; run;

I expect the division operation should fail in both the cases since the operation is not between numeric values. First case works. Second case fails execution as expected. Can someone please explain the difference ? I am using SAS 9.04.

Case 1 (Working):

data work.products; Product_Number = 11; Item = '12'; Item_Reference = Item/Product_Number; run;

Case 2 (Not working):

data work.products; Product_Number = 11; Item = '12'; Item_Reference = Product_Number/Item; run;

最满意答案

如果案例1有效,但案例2没有,那必须是涉及SAS 9.0的某种错误。 如果检测到字符串是数字,SAS应自动将字符值转换为数字。 您的代码在SAS 9.4中没有问题。 由于自动类型转换不适合您,您可以使用input函数或将值乘以1轻松解决问题。

data work.products; Product_Number = 11; Item = '12'; Item2 = input(Item, 8.); Item_Reference = Product_Number/Item2; run; data work.products; Product_Number = 11; Item = '12'; Item2 = Item*1 Item_Reference = Product_Number/Item2; run;

This must be some sort of bug involving SAS 9.0 if case 1 works but case 2 doesn't. SAS should convert character values to numeric automatically if it detects that the string is a number. Your code works without a problem in SAS 9.4. Since automatic type conversion is not working for you, you can easily solve the problem by using the input function, or by multiplying the value by 1.

data work.products; Product_Number = 11; Item = '12'; Item2 = input(Item, 8.); Item_Reference = Product_Number/Item2; run; data work.products; Product_Number = 11; Item = '12'; Item2 = Item*1 Item_Reference = Product_Number/Item2; run;

更多推荐