SAS ALTER TABLE 修改长度

SAS ALTER TABLE MODIFY Length

假设我在 SAS someTable 中有一个类型为 Character 的列 someColumn

我可以通过以下方式调整长度、格式、信息格式和标签:

ALTER TABLE WORK.someTable
   MODIFY someColumn char(8) format=$CHAR6. informat=$CHAR6. label='abcdef'

但我怀疑这是否是正确的方法,原因如下:

实际上,我希望以下代码能够工作:

ALTER TABLE WORK.someTable
   MODIFY someColumn length=8 format=$CHAR6. informat=$CHAR6. label='someLabel'

这段代码运行没有错误螺母不会改变长度。

问题: 使用 ALTER TABLE / MODIFY 修改列的长度的正确语法是什么? (对于任意列类型,如 character/numeric/date。)

定义更改变量(“列”)的语法与 PROC SQL 用于定义变量的语法相同。文档中所谓的“列定义组件”

column data-type <column-modifier(s)>

这就是为什么要使用 SQL 语法、char(n) 或 num 来指定类型的原因。请注意,SAS 数据集只有两种数据类型:固定长度的字符串和浮点数。 SAS 会自动将任何其他 SQL 数据类型转换为正确的数据类型。

文档中详细说明了更改类型的限制:

Changing Column Attributes If a column is already in the table, then you can change the following column attributes by using the MODIFY clause: length, informat, format, and label. The values in a table are either truncated or padded with blanks (if character data) as necessary to meet the specified length attribute.

You cannot change a character column to numeric and vice versa. To change a column’s data type, drop the column and then add it (and its data) again, or use the DATA step.

Note: You cannot change the length of a numeric column with the ALTER TABLE statement. Use the DATA step instead.

请注意,要对数据集进行此类更改,SAS 必须创建一个全新的数据集。所以你不妨只写一个数据步骤来创建新的数据集,然后你将拥有完全的控制权。

如果更改字符变量的长度也要小心,以确保附加的格式仍然正确。

在您的示例中,您将变量更改为 8 个字节长,但附加的格式仅显示前 6 个字节。

一般来说,字符变量最好不要附上格式,以免类型不匹配造成混淆。遗憾的是,无法使用 PROC SQL 删除附加格式。最好的办法是将格式设置为 $.,即没有明确的宽度。如果您想完全删除格式,您需要在 PROC DATASETS 或数据步骤中使用 FORMAT 语句。