将数据集sas的值传递给宏

时间:2015-01-22 12:18:02

标签: sas sas-macro

我正在学习sas宏语言,但我遇到了问题 我写了一个宏:

%macro avg_acceleration(mark_type, avg_acceleration, mark_angle);
%if &mark_type='A' %then %sysfunc(abs(&avg_acceleration/%sysfunc(cos(%sysfunc(abs(&mark_angle*( 3.1415926535897/180.0)))))));
%else %if &mark_type='C' %then %sysfunc(abs(&avg_acceleration/%sysfunc(sin(%sysfunc(abs(&mark_angle*( 3.1415926535897/180.0)))))));
%mend avg_acceleration;

然后我要做的是在另一个宏中启动一个宏:

%macro statistiche;
data pippo;
set prova;
a = mark_type;
b= avg_acceleration;
c = mark_angle;
avg_acc=%avg_acceleration(a, b, c);
run;
%mend statistiche;

%statistiche;

但我有这个错误:

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, a missing value, INPUT, PUT.

不可能以这种方式使用宏吗?做我想要的正确方法是什么?谢谢

2 个答案:

答案 0 :(得分:1)

不确定为什么要2个宏来执行此操作。为什么不只是一个简单的数据步骤?

data work.test;
set work.prova;
if mark_type='A' then do;
    avg_acc=abs(avg_acceleration)/(cos(abs(mark_angle)*(constant('pi')/180.0)));
end;
if mark_type='C' then do;
    avg_acc=abs(avg_acceleration)/(sin(abs(mark_angle)*(constant('pi')/180.0)));
end;
run;

答案 1 :(得分:0)

替代数据步骤,重复性较少的代码:

data work.test;
  set work.prova;
  select (mark_type);
    when('A') phase = 0;
    when('C') phase = 90;
  end;
  avg_acc=abs(avg_acceleration)/cos((mark_angle + phase)*constant('pi')/180.0);
run;

如果您希望捕获此逻辑以便在其他数据步骤中重用,并且可以选择使用不同的数据集变量代替原始数据,则可以执行以下操作:

%macro avg_acceleration(mark_type        =mark_type,
                        avg_acceleration =avg_acceleration,
                        mark_angle       =mark_angle,
                        avg_acc          =avg_acc
                       );
      select (&mark_type);
        when('A') phase = 0;
        when('C') phase = 90;
      end;
      &avg_acc=abs(&avg_acceleration)/cos((&mark_angle + phase)*constant('pi')/180.0);
%mend;

然后你应该能够按原先的预期调用它或更少:

data work.test;
  set work.prova;
  %avg_acceleration;
run;