sql - 如何检测数据集中的所有空列并删除\删除它们?

标签 sql sas

正如标题中所建议的,我想删除所有空列\变量(其中所有记录都为空或等于 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;

鉴于以下原因,执行需要相当长的时间:
  • 旧列的数量很大
  • 事实上我需要在另一个数据集中做一个循环来设置 之后的数字oldcol

  • 列号
     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 &noteopt.;
    %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/

    相关文章:

    mysql - 如何在 SAS 中将表作为参数值传递给 proc sql?

    sql - 我怎么能有像 SELECT id, (id in(2,3,4) ) as idIs234 来自 Messageset

    python - 使用 SQLAlchemy 更快地将记录插入表中

    mysql - 随机化查询中每行两列的顺序

    xml - XML 映射的 PATH 元素中的 POSITION() 函数在 XML 映射器中有效,但在 SAS 代码中无效

    sas - 什么是 SAS 格式 8.

    loops - SAS 组合数据集、二进制搜索、索引

    sql - 如何从历史数据中检索行程?

    MYSQL创建触发器时出错

    java - PC SAS 和服务器 SAS 之间的区别