我目前正在尝试练习SAS宏,虽然它们在很大程度上看起来是合乎逻辑的,但我在文档中几乎没有发现关于如何使用宏提高阵列效率的信息。有没有另一种方式,而我做这一切都错了?我想在工作中改进一些SAS代码,所以这只是一个简单的例子,我学习了如何做到这一点
这是我最初的基本阵列代码:
data dataset_new;
set dataset_old;
array array_one{12} value01-value12;
do i = 1 to 12;
if array_one{i} = '121' then sponsor = 'yes';
if array_one{i} in ('44', '55') then participant = 'active';
end;
run;
这是我尝试在其中添加宏的拙劣尝试。
%let maximum = 10;
%MACRO SPORTS(START,maximum);
data dataset_new;
set dataset_old;
array array_one{12} value01-value12;
%DO i = &START %TO &maximum ;
if array_one{i} = '121' then sponsor = 'yes';
if array_one{i} in ('44', '55') then participant = 'active';
%END;
%MEND SPORTS;
run;
谢谢你对如何做到这一点的任何想法。
您正在混合作用域,这通常是不可取的。
你似乎想要的所谓改进是什么?
%do
循环将为宏%do
的每次迭代生成2个数据步长if语句的源代码。
宏外部的全局maximum
赋值与设置或重写宏调用应传递的maximum
无关。任何事情都必须调用宏SPORTS
,否则您只是在编译宏。宏定义也奇怪地与宏定义之外的run;
交错。温柔地说,你做错了。
宏生成源代码,因此不能更改正在运行的源代码(因此已经编译了数据步骤)
至少在理论上,你可能想要
if array_one{&i} = '121' then sponsor = 'yes';
而不是
if array_one{i} = '121' then sponsor = 'yes';
但从更广泛的意义上来说,这确实没有帮助。
你真的试图评估之间的差异吗
do i = 1 to 12;
if array_one{i} = '121' then sponsor = 'yes';
if array_one{i} in ('44', '55') then participant = 'active';
end;
和宏生成的源
if value01 = '121' then sponsor = 'yes';
if value01 in ('44', '55') then participant = 'active';
if value02 = '121' then sponsor = 'yes';
if value02 in ('44', '55') then participant = 'active';
if value03 = '121' then sponsor = 'yes';
if value03 in ('44', '55') then participant = 'active';
if value04 = '121' then sponsor = 'yes';
if value04 in ('44', '55') then participant = 'active';
if value05 = '121' then sponsor = 'yes';
if value05 in ('44', '55') then participant = 'active';
if value06 = '121' then sponsor = 'yes';
if value06 in ('44', '55') then participant = 'active';
if value07 = '121' then sponsor = 'yes';
if value07 in ('44', '55') then participant = 'active';
if value08 = '121' then sponsor = 'yes';
if value08 in ('44', '55') then participant = 'active';
if value09 = '121' then sponsor = 'yes';
if value09 in ('44', '55') then participant = 'active';
if value10 = '121' then sponsor = 'yes';
if value10 in ('44', '55') then participant = 'active';
if value11 = '121' then sponsor = 'yes';
if value11 in ('44', '55') then participant = 'active';
if value12 = '121' then sponsor = 'yes';
if value12 in ('44', '55') then participant = 'active';