sas - 从数值列变量中删除无效字符串条目 (\n)
问题描述
我有一组 A1-A54 列。所有这些列都应该被格式化为数字。但是,这些列中的某些条目存储为“\N”,这会破坏列格式,并且不允许在列上运行类似 proc 的操作。想了解如何通过在所有列上运行循环来将这些值设置为缺失。
我尝试在所有列上使用数组数字运行 do 循环,但由于 \N 存在于某些列中,它们被格式化为字符串变量并且数组方法不起作用。
array nums[*] _numeric_;
if nums[i]='\N' then nums[i]=.;
end;
这不会将 \N 转换为缺失值,因为 SAS 将它们解释为数字变量中的字符串并引发错误。
在数值列中找到错误字符串变量。在运行循环时。
解决方案
查找非数字数据的导入过程将导致该列为_character_
,因此您的一些A1-A54
是字符。
如果你编写这个代码会发生什么?
array mydata A1-A54;
你有看到
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
您将需要识别A
字符列并将它们的值转换为类似命名的数字(如果可能),当转换不可能时,数字值将丢失(如预期的那样)。
例子:
data have;
length a1 8 a2-a4 $15; %* pretend the import created a mixed bag of types;
row = 1;
a1 = 123;
a2 = '123' || byte(10) || '456';
a3 = byte(10);
a4 = '123';
output;
row = 2;
a1 = 456;
a2 = '789' || byte(10) || 'XYZ';
a3 = '987';
a4 = byte(10);
output;
run;
proc contents noprint data=have out=have_meta;
run;
proc sql noprint;
select
cats(name,'= char',name),
cats(name,'= input(char', name,',??best12.);'),
'char'||name
into
:rename separated by ' ',
:convert separated by ' ',
:drop separated by ' '
from have_meta
where
type=2 and
(substr(name,1,1) in ('a', 'A')) /* the characters of the A-team :) */
;
quit;
options symbolgen;
data want(drop=&drop);
set have (rename=(&rename));
&convert
run;
options nosymbolgen;
推荐阅读
- java - 为什么 List.contains(Object) 的行为不同?
- r - 如何在同一个数据帧上做colsum和average
- powerbi - 使用 DAX 生成日期系列
- javascript - 仅在服务器上需要一个包
- java - Intellij IDEA 仅针对所有未提交的更改运行测试
- regex - 在换行符之前匹配字符,不包括空格?
- macos - MacOS - 使用带有launchd的脚本 - 启动,登录,注销,关闭?
- service-worker - Workbox 的服务人员在更改时未更新
- python - AllenNLP 共指分辨率的多 GPU 训练
- reactjs - 事件处理程序中带有 [name] 的 PrevState