在没有警告的情况下更改可变长度

Changing a variable length without getting a warning

我在数据集 OLD 上有一个名为 ID 的变量,我需要将长度从 13 更改为 12 以进行合并。以下是与此变量关联的元数据。

Variable Type Len Format Informat Label 
ID       Char  13   .     . 'Person ID'

(ID的每个值的内容在这个文件中总是恰好12个字符)

当我尝试在 DATA 步中编辑 set 语句之前的长度时,我收到警告。当我同时编辑长度和格式时,也会发生同样的情况。

data NEW;
length ID ;
format ID .;
set OLD;
run;

...
WARNING: Multiple lengths were specified for the variable AN_RESEARCHID by 
             input data set(s). This can cause truncation of data.

SAS 社区论坛中的示例似乎没有描述为什么会出现此警告,或者如何避免它。有什么想法吗?

您可以重命名 SET 语句中的原始变量,并创建一个具有所需长度的新变量。然后在数据步骤中将新变量设置为等于旧变量。你不会得到警告。但当然,它警告你的内容仍然存在——也就是说,如果你的数据恰好有 13 个字符,它会在将它塞入一个更短长度的变量时截断该值。

data NEW(drop=ID_orig);
length ID ;
set old(rename=(ID=ID_orig));
ID= ID_orig;
run;

A​​mw5G给出的答案是,如果不能修正ID原来的长度设置,我会如何解决这个问题。但是,您应该研究在前面的步骤中更改原始长度分配。

变量收到不需要的长度的一些原因是:

如果一个字符 VAR 被分配给一个没有 LENGTH statement then the VAR will take the length of the first value assigned to it. EXAMPLE

的值

如果字符 VAR 由函数赋值,例如 SCAN, it can receive a length of 200. The CAT function will return a length of the sum of the lengths of the values which are being concatenated. EXPLANATION

有一个名为 varlenchk 的 SAS 选项。通过暂时将其设置为 NOWARN,您可以在没有任何警告的情况下更改长度。

options varlenchk = nowarn; * Turn off the warning prior to your operation;

/* your operations here */

options varlenchk = warn; * put it back on to be safe!;

有关详细信息,请参阅此 link:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003269301.htm