SAS:使用宏提高阵列效率

时间:2019-02-05 01:51:22

标签: arrays sas sas-macro

此刻,我正在尝试练习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;        

这是我la脚的尝试,试图在其中添加宏。

%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;        

感谢您对执行此操作的任何想法。

1 个答案:

答案 0 :(得分:1)

您正在混合示波器,这通常是不希望的。

您似乎想要什么所谓的改进?

%do循环将为宏%do的每次迭代的语句生成2个数据步骤的源代码。

宏外部的全局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';
相关问题