选择所有缺少值的字符变量 [英] Select character variables that have all missing values

查看:18
本文介绍了选择所有缺少值的字符变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约3000个变量的SAS数据集,我想去掉所有值都丢失的字符变量。我知道如何对数值变量执行此操作--我特别想知道字符变量。我需要使用基本SAS来完成这项工作,但这可能包括proc SQL,这也是我将这一个也标记为"SQL"的原因。
谢谢!

编辑:
背景资料:这是一个很高的数据集,有7波采访的调查数据。一些(但不是全部)调查项目(变量)跨浪重复。我正在尝试创建每个Wave中实际使用的项目的列表,方法是提取该Wave的所有记录,删除除SAS的缺省缺失值之外什么都没有的所有列,然后运行proc contents

推荐答案

我创建了一个宏,该宏将检查空字符列,并将它们从原始数据集中删除,或者创建一个删除了空列的新数据集。它有两个可选参数:数据集的名称(默认值为最近创建的数据集)和命名新副本的后缀(将Suffix设置为Nothing以编辑原始副本)。

它将proc freq与级别选项和自定义格式一起使用来确定空字符列。然后使用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 */

这篇关于选择所有缺少值的字符变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆