将数据集 sas 的值传递给宏

pass value of the dataset sas to a 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 个宏来执行此操作。为什么不只是一个简单的数据步骤?

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;

具有较少重复代码的替代数据步骤:

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;