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

问题描述 投票:0回答:2

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

%let M=N;
proc sql;
select states,profit,
case
   when  .....
   else
   end
from geography_dim 
quit;
sql sas
2个回答
2
投票

为了便于论证,假设你将宏变量的名称改为:"M=N",但只返回以M开头的状态的名称,如果宏变量等于其他变量,则使用...。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 如果你必须这样做,然而代码会有些不透明。


1
投票

它不是SQL,但在datastep非常简单。如果你想检查盯着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
    */
© www.soinside.com 2019 - 2024. All rights reserved.