如果宏变量m=n,我需要选择不以m开头的状态名称,但如果宏变量等于使用条件处理的任何其他变量,则只返回以m开头的状态名称。例如:
%let M=N;proc sql;select states,profit,case when ..... else endfrom geography_dim quit;
%let M=N;
proc sql;
select states,profit,
case
when .....
else
end
from geography_dim
quit;
nbnkbykc1#
为了便于讨论,假设您更改了宏变量的名称 M 更可笑的表达,例如 YN_OPTION_SELECT_M_STATES ```%let YN_OPTION_SELECT_M_STATES = N;
M
YN_OPTION_SELECT_M_STATES
proc sql;select states,profit,casewhen .....elseendfrom 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` 但是,如果必须这样做,代码将有点不透明。
41zrol4v2#
它不是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 */
/*test data*/
data geography_dim ;
states="Aaaaa";profit=10;output;
states="Naaaa";profit=10;output;
run;
/*set macro variable*/
/*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;
/* results
states profit
Aaaaa 0
Naaaa 100
*/
2条答案
按热度按时间nbnkbykc1#
为了便于讨论,假设您更改了宏变量的名称
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;
41zrol4v2#
它不是sql,但在datastep中非常简单。如果您想用m宏值检查起始值,在这种情况下为“n”,您可以这样做: