正如标题中所建议的,我想删除所有空列\变量(其中所有记录都为空或等于 null 或“”),以减少以后执行的时间成本。
详细场景:
我有一个包含 1000 列的 dataset(),其中一些\很多是空的。现在我想创建一个新的数据集,我需要在以前数据集的某些条件下添加列。
data new;
set old;
if oldcol1 ne "" then newcol1='<a>'||strip(oldcol1)||'</a>';
end;
if oldcol2 ne "" then newcol2='<a>'||strip(oldcol2)||'</a>';
end;
...
...;
drop oldcol1 oldcol2.....oldcol1000;
run;
鉴于以下原因,执行需要相当长的时间:
列号
1
2
3
...
1000
所以你可以想象在搜索、查找和设置值方面要执行多少次。
因此,我能想到的一种减少时间成本的方法是首先删除所有空列。但是,任何有关优化算法的输入也非常受欢迎。
谢谢
最佳答案
这是一个通用宏,可用于生成源数据集中空列的列表,然后可以将其传递给 drop 语句。它使用 proc 格式和 proc freq,因此速度相对较快。
%macro findmiss(ds,macvar);
%local noteopt;
%let noteopt=%sysfunc(getoption(notes));
option nonotes;
*ds is the data set to parse for missing values;
*macvar is the macro variable that will store the list of empty columns;
%global &macvar;
proc format;
value nmis .-.z =' ' other='1';
value $nmis ' '=' ' other='1';
run;
ods listing close;
ods output OneWayFreqs=OneValue(
where=(frequency=cumfrequency
AND CumPercent=100));
proc freq data=&ds;
table _All_ / Missing ;
format _numeric_ nmis.
_character_ $nmis.;
run;
ods listing;
data missing(keep=var);
length var $32.;
set OneValue end=eof;
if percent eq 100 AND sum(of F_:) < 1 ;
var = scan(Table,-1,' ');
run;
proc sql noprint;
select var into: &macvar separated by " "
from missing;quit;
option ¬eopt.;
%mend;
以下是您可以如何使用它:
%findmiss(old,droplist); /*generate the list of empty columns */
data new;
set old(drop=&droplist);
run;
关于sql - 如何检测数据集中的所有空列并删除\删除它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5482739/