繁体   English   中英

使用宏变量在proc sql(SAS)中进行条件处理

[英]Conditional processing in proc sql(SAS) using a macro variable

如果宏变量 M=N,我需要 select 不以 M 开头的状态名称,但如果宏变量等于使用条件处理的任何其他变量,则仅返回以 M 开头的状态名称。 例如:

%let M=N;
proc sql;
select states,profit,
case
   when  .....
   else
   end
from geography_dim 
quit;

为了论证起见,假设您将宏变量M的名称更改为更具表现力的名称,例如YN_OPTION_SELECT_M_STATES

%let YN_OPTION_SELECT_M_STATES = N;

proc sql;
select states,profit,
case
   when  .....
   else
   end
from geography_dim

/* add this */
where 
  ("&YN_OPTION_SELECT_M_STATES" eq 'N' & STATE not like 'M%')
  or
  ("&YN_OPTION_SELECT_M_STATES" ne 'N' & STATE     like 'M%')

;
quit;

如果必须,请恢复为宏变量M ,但是代码会有些不透明。

它不是 SQL 但在数据步中非常简单。 如果您想在这种情况下使用 M 宏值检查“N”的凝视,您可以这样做:

    /*test data*/
    data geography_dim ;
    states="Aaaaa";profit=10;output;
    states="Naaaa";profit=10;output;
    run;
    /*set macro variable*/
    %let M=N;
    /*check if you want*/
    %put "&M";
    /*your case in datastep*/
    data test;
     set geography_dim;
     if substr(states,1,1) eq "&M" then profit=profit*10;
     else profit=0;
    run;
    /* results
    states  profit
    Aaaaa   0
    Naaaa   100
    */

暂无
暂无

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

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