简体   繁体   English

使用SAS中的PROC SQL选择变量范围

[英]Select range of variables with PROC SQL in SAS

I want to select rows based on a range of similar variables meet certain conditions: 我想根据一系列类似变量选择符合某些条件的行:

proc sql;
  create table2 as
  select * from table1 
  where proc1 in ('111', '222', '333') or
        proc2 in ('111', '222', '333') or
        proc3 in ('111', '222', '333');
quit;

Is there a way I can select the variables more efficiently? 有没有办法可以更有效地选择变量? In SAS' data step , I can use proc1-proc3 , but this can't be done in proc SQL . 在SAS的data step ,我可以使用proc1-proc3 ,但这不能在proc SQL完成。

It is not possible to do exactly that, but there are some options that will probably suite you. 不可能完全做到这一点,但有一些选项可能会适合你。

First write a sql statement that writes these conditions to a macro variable proc_conditions 首先编写一个sql语句,将这些条件写入宏变量proc_conditions

proc sql;
  select compbl(name ||' in ('111', '222', '333')')
  into : proc_conditions separated by ' or '
  from sasHelp.vcolumn
  where libName = 'WORK'
    and memName = 'TABLE2'
    and upcase(Name) like 'PROC%'
  ;

Note that libName's and memName's are always uppercase in the meta data, while field Name's or mixed case (but case insensitive). 请注意,libName和memName在元数据中始终为大写,而字段名称或混合大小写(但不区分大小写)。 I added Compbl to reduce multiple blanks to singles, but that is not required. 我添加了Compbl以减少多个空白到单打,但这不是必需的。

You might check the result is proc1 in ('111', '222', '333') or proc2 in ('111', '222', '333') or proc3 in ('111', '222', '333') by writing it to the log. 您可以检查结果是proc1 in ('111', '222', '333') or proc2 in ('111', '222', '333') or proc3 in ('111', '222', '333')将其写入日志。

  %put &proc_conditions;

Then just use it 然后就使用它

  create table2 as
  select * from table1 
  where &proc_conditions;
quit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM