SAS:使用宏提高数组效率

SAS: improve efficiency of array with macros

我目前正在尝试练习 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';