我创建了一个宏来检查空字符列,并将它们从原始列中删除,或者创建一个删除了空列的新数据集。它有两个可选参数:数据集的名称(默认是最近创建的数据集)以及用于命名新副本的后缀(将后缀设置为无需编辑原始数据)。
它使用proc freq与levels选项和自定义格式来确定空字符列。 proc sql然后用于创建要删除的列的列表并将它们存储在宏变量中。
下面是宏:
%macro delemptycol(ds=_last_, suffix=_noempty);
option nonotes;
proc format;
value $charmiss
' '= ' '
other='1';
run;
%if "&ds"="_last_" %then %let ds=&syslast.;
ods select nlevels;
ods output nlevels=nlev;
proc freq data=&ds.(keep=_character_) levels ;
format _character_ $charmiss.;
run;
ods output close;
/* create macro var with list of cols to remove */
%local emptycols;
proc sql noprint;
select tablevar into: emptycols separated by ' '
from nlev
where NNonMissLevels=0;
quit;
%if &emptycols.= %then %do;
%put DELEMPTYCOL: No empty character columns were found in data set &ds.;
%end;
%else %do;
%put DELEMPTYCOL: The following empty character columns were found in data set &ds. : &emptycols.;
%put DELEMPTYCOL: Data set &ds.&suffix created with empty columns removed;
data &ds.&suffix. ;
set &ds(drop=&emptycols);
run;
%end;
options notes;
%mend;
例子用法:
/* create some fake data: Here char5 will be empty */
data chardata(drop= j randnum);
length char1-char5 $8.;
array chars(5) char1-char5;
do i=1 to 100;
call missing(of char:);
randnum=floor(10*ranuni(i));
do j=2 to 5;
if (j-1)<randnum<=(j+1) then chars(j-1)="FOO";
end;
output;
end;
run;
%delemptycol(); /* uses default _last_ for the data and "_noempty" as the suffix */
%delemptycol(ds=chardata, suffix=); /* removes the empty columns from the original */
我认为你需要一个更好的解释你的问题,以获得人们的污名。也许一些示例数据等 – 2010-07-16 19:21:43
嗨路易莎 - 请你详细说明你是如何做到这一点的数字列?谢谢! – 2010-07-18 21:44:26
嗨Rob--非常感谢你 - 我今天晚些时候会发布代码,以及我周末的另一个想法,如果它能工作... – 2010-07-19 14:38:02