procsql(sas)中使用宏变量的条件处理

gdx19jrr  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(382)

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

  1. %let M=N;
  2. proc sql;
  3. select states,profit,
  4. case
  5. when .....
  6. else
  7. end
  8. from geography_dim
  9. quit;
nbnkbykc

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;

  1. 还原为宏变量 `M` 但是,如果必须这样做,代码将有点不透明。
展开查看全部
41zrol4v

41zrol4v2#

它不是sql,但在datastep中非常简单。如果您想用m宏值检查起始值,在这种情况下为“n”,您可以这样做:

  1. /*test data*/
  2. data geography_dim ;
  3. states="Aaaaa";profit=10;output;
  4. states="Naaaa";profit=10;output;
  5. run;
  6. /*set macro variable*/
  7. %let M=N;
  8. /*check if you want*/
  9. %put "&M";
  10. /*your case in datastep*/
  11. data test;
  12. set geography_dim;
  13. if substr(states,1,1) eq "&M" then profit=profit*10;
  14. else profit=0;
  15. run;
  16. /* results
  17. states profit
  18. Aaaaa 0
  19. Naaaa 100
  20. */
展开查看全部

相关问题