SAS:根据另一个变量分配变量

时间:2015-07-27 19:44:20

标签: select if-statement sas

我正在努力学习SAS。我在其中一个程序中看到以下代码:

data _null_;
    set Rates;
    IF attained_age = '<60'   then call symput('U_60_Alpha',count);
    IF attained_age = '<60'   then call symput('U_60_Beta',exposure);
    IF attained_age = '60-64' then call symput('U_60_64_Alpha',count);
    IF attained_age = '60-64' then call symput('U_60_64_Beta',exposure);
    IF attained_age = '65-69' then call symput('U_65_69_Alpha',count);
    IF attained_age = '65-69' then call symput('U_65_69_Beta',exposure);
run;

写一个更好的方法是这样的吗?

data _null_;
    set Rates;
    select (attained_age);
        when('<60');
          do;
             call symput('U_60_Alpha',count);
             call symput('U_60_Beta',exposure);
          end;
        when('60-64');
          do;
             call symput('U_60_64_Alpha',count);
             call symput('U_60_64_Beta',exposure);
          end;
        when('65-69');
          do;
             call symput('U_65_69_Alpha',count);
             call symput('U_65_69_Beta',exposure);
          end;
    end;
run;

不确定这是否比前一个块更好或更差 - 当然需要更多的垂直空间,但可能运行得更快(更少需要比较)。还有其他优点/缺点吗?从你的角度来看哪一个会更好?

1 个答案:

答案 0 :(得分:1)

如果它是有效的SAS代码,则第二种效率更高。我不习惯看到那样的事情而且不想现在就开始测试。

您的变量似乎是调用symput中变量名称的一部分。我会创建一个前缀,并在我的call symput代码中使用它。我认为这更容易维护和阅读。

if attained_age='<60' then prefix='U_60';
else if attained_age='60-64' then prefix='U_60_64';
else if attained_age='65-69' then prefix='U_65_69';

call symput(prefix||"_Alpha", count);
call symput(prefix||"_Beta", exposure);

另一种方法是定义应用前缀的格式:

proc format;
value $ prefix
'<60' = 'U_60'
'60-65' = 'U_60_65'
'65-69' = 'U_65-69'
;
run;

然后在您的数据步骤中:

call symput(put(attained_age, $prefix.)||"_Alpha", count);
call symput(put(attained_age, $prefix.)||"_Beta", exposure);

这些都不具备重要意义。速度优势,除非你有大量数据集 - 我的猜测是数千万。