如何在 SAS 中将字符串转换为数字?

How Do I Convert A String Into a Number in SAS?

我是 SAS 的新手,正在为类型转换而苦苦挣扎。对我来说,它们似乎一点都不直观。 我需要在 SAS PROC SQL 语句中执行此操作。

谁能帮我找出将格式如下所示的字段转换为数字的最佳方法,以便它可以执行一些 addition/subtraction 并与数字字段进行比较?

字段格式示例:“+3,820.00”

我已经使用 strip() 去掉 + 号、逗号和空格,然后使用 input() 把它变成一个数字,但我没有任何运气。

使用压缩而不是剥离

field1=input(compress(field,'+,'),6.);

field1=input(compress(field,'+,'),comma10.2)format=comma10.2;

输入是将字符串转换为数字的正确方法,您可以在 proc sql select 语句中使用输入(参见 character to numeric conversion while sql extracting for example). There are informats that you can pass to the input function that will help you with commas & signs so that you don't have to strip them out prior to calling input. More information can be found here: Informats by Category.

如果这没有帮助,请告诉我。

commaw.d 信息格式将处理所有这些,而无需任何 stripping/etc。它很乐意将 + 号视为正号,将 comma/decimal 视为正号。这也适用于 SQL,但更容易显示如下示例...

data _null_;
  x=' +3,820.00';
  y=input(x,comma12.);
  put x= y=;
run;