【问题标题】:How to do a "where x in ..." statement in a SAS data step如何在 SAS 数据步骤中执行“where x in ...”语句
【发布时间】:2016-02-25 01:09:29
【问题描述】:

我有一个大型 SAS 数据表,其中包含针对我的整个基本客户群 (Master_v01) 的一系列指标。我有一个单独的数据表,其中包含我要从分析中排除的客户列表 (Excluded_Cust)。

是否可以这样做:

data Master_v02;
set Master_v01;
where cust not in (select cust from Excluded_Cust);
run;

这可能吗?

如果不是,那么实现我的预期目标的最佳方法是什么?让我的基表以及那些我想从数据中排除的客户?

【问题讨论】:

    标签: sql sas proc


    【解决方案1】:

    这是使用带有 where 子句的宏变量的另一种方法:

    proc sql noprint;
        select
            quote(cust)
        into
            :cust_exclude
        separated by
            " or cust ne "
        from
            Excluded_Cust
        ;
    quit;
    
    data Master_v02;
        set Master_v01;
        where cust ne &cust_exclude;
    run;
    

    【讨论】:

      【解决方案2】:

      我的数据步骤代码有点生疏,但您可以通过查找值并检查匹配来做到这一点。我认为是这样的:

      data Master_v02;
          set Master_v01;
          set Excluded_Cust key=cust;
          if _iorc_ = 0 then delete;
      run;
      

      当然,推荐在Excluded_Cust 中的cust 上建立索引。

      【讨论】:

        【解决方案3】:

        我能想到的两种方法:

        1.PROC SQL

        proc sql;
          create table Master_v02 as
            select * 
              from Master_v01
              where cust not in (select cust from Excluded_cust);
        quit;
        

        2.数据步合并

        首先对 Master_V01 和 Excluded_cust 数据集按 cust 排序,然后:

        data Master_V02;
          merge Master_V01 (in=Mast)
                Excluded_cust (in=Excl);
          by cust;
          if Mast and not Excl;
        run;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-23
          • 2010-12-23
          相关资源
          最近更新 更多