select 作为字符串的数字列

select a numeric column as string

是否可以在 sas 中使用 proc sql select 将数字列作为字符串?

例如我有这个 table (table_1):

+------+------+------+--------------+--+
| id_1 | id_2 | id_3 |     date     |  |
+------+------+------+--------------+--+
|    3 |    7 |    3 |  25/06/2017  |  |
|    4 |   11 |    9 |  25/06/2020  |  |
+------+------+------+--------------+--+

id_1、id_2 和 id_3 可以是数字或字符串。

我想创建一个 table (table_2),其中这三列的类型应该是字符串

我写了这段代码:

proc sql;

Create table table_2 as 

select date, Convert(varchar(30),a.id_1), Convert(varchar(30),a.id_2), Convert(varchar(30),a.id_3)

from table_1   a

;quit;

但是不行

试试吧...

SELECT date, CAST(a.id_1 AS varchar(30)), CAST(a.id_2 AS varchar(30)), CAST(a.id_3 AS varchar(30))
FROM table_1   a

最好的解决方案是修复您的上游流程,以始终使用一致的类型生成变量。

在 SAS 中,您可以使用 PUT() 函数将值转换为字符串,只需使用适合变量类型的格式即可。例如,如果您的变量是数字,但它应该是 9 位长且带有重要的前导零,那么您会想要使用 Z9。格式来转换它并表示前导零。

 select put(id_1,z9.) as id_1  

如果您想在不知道变量类型的情况下将数字或字符变量转换为字符串,您可以使用 CATS() 函数。但是你将无法控制数字如何转换为字符串。使用 LENGTH= 属性强制 SAS 定义长度为 30 的变量。

 select cats(id_1) as id_1 length=30