需要对数据进行全面扫描以检查所有列中的所有值。在扫描期间,出现的值不是"Null"将排除该列作为删除候选。
您可以使用一个临时数组来跟踪字符列名称是什么,并将另一个数组设置为,_CHARACTER_以便可以在每一行中迭代这些列。该过程将构建一个可以根据您的标准删除的列列表——该列表被放置在宏符号表中,并且可以在后续代码中使用,要么在没有这些列的情况下重写数据,要么简单地删除它们在使用数据集选项进行处理期间。
DATA test;
INPUT name$ favourite_food$ occupation$ favourite_sport$;
CARDS;
John Null Nurse Null
Michelle Null Lawyer Null
Peter Null Teacher Null
Kai Null Doctor Null
Zonker Null Null Null
run;
%let DROP_VARS=;
data _null_;
set test end=end;
array char_vars _CHARACTER_; * for iterating over values;
array null_vars (1000) $32 _temporary_ ; * for tracking column names;
* populate column name tracking array;
if _n_ = 1 then do;
do index = 1 to dim(char_vars);
null_vars(index) = vname(char_vars(index));
end;
end;
* scan each row, iterating over character variables;
* remove a column name from drop consideration when non "Null" occurs;
do index = 1 to dim(char_vars);
if not missing(null_vars(index)) then
if char_vars(index) ne "Null" then
null_vars(index) = '';
end;
* place space separated list of columns containing only "Null" in macro symbol table;
if end then
call symput('DROP_VARS', catx(' ', of null_vars(*)));
run;
* use macro variable as desired;
%put NOTE: &=DROP_VARS;
proc print data=test(drop=&DROP_VARS);
title "Non-null columns of TEST";
run;
data TEST2(label="Copy of Test, excluding null columns");
set TEST;
drop &DROP_VARS;
run;
还有许多其他 SAS 方法来编写解决方案以删除具有所有相同值的列- 搜索它们!