0

我找不到此错误的解决方案。我尝试了 usign %eval、%sysfunc 和 %sysevalf,但没有成功。正确评估宏中的“&set”需要什么?

%macro set_istituzionale(set=, varout=);
  %if &set/100 = 1 %then &varout = 'AP';
%mend set_istituzionale;

data soff2; set soff; %set_istituzionale(set=setcon,varout=set); run;

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
       &set/100 = 1
ERROR: The macro SET_ISTITUZIONALE will stop executing.
4

1 回答 1

1

你宏观背后的逻辑是错误的。

您在数据步内调用该宏,因此该宏应包含可以在数据步内解析的内容(语句)。

If-else 不会用宏语言编写,而是用普通的数据步语言编写。

%macro set_istituzionale(set=, varout=);
  if &set/100 = 1 then &varout = 'AP';
%mend set_istituzionale;

现在,如果您使用您的电话,您将通过以下方式解决您的数据步骤:

data soff2; 
set soff;
%set_istituzionale(set=setcon,varout=set);
run;

将变成:

data soff2; 
set soff;
if setcon/100 = 1 then set = 'AP';
run;

在您的代码中,您使用的是宏代码,因此您的步骤在内部被解析为宏布尔语句 %if &set/100 解析为 %if setcon/100 其中 setcon 是一个字符串(这里我们有条件地使用宏语言,写下该变量不会捕获变量的值,因为它完全独立于数据步)。

你应该只考虑宏语言可以为你写下代码的东西,例如像你第一次尝试这样的东西可以用来有条件地插入一个语句到一个宏变量的值,比如:

data try;
set want;

%if &macrovar=A %then %do;
if var1=1 and var2='B' then var3='C';
%end;

%if &macrovar=B %then %do 
if var1=2 and var2='A' then var3='D';
%end;

run;

when the macro variable macrovar will be = A the step will be:

data try;
set want;
if var1=1 and var2='B' then var3='C';
run;

if the macro variable macrovar will be = B the step will be:

data try;
set want;
if var1=2 and var2='A' then var3='D';
run;

但是您也可以在 datastep 之外使用宏代码,例如执行 datastep 或其他有条件的宏变量的值:

%if &macrovar=A %then %do;

data try; set tr;
some code;
run;

%end;

%else %if &macrovar=B %then %do;

proc sort data=tr out=try nodupkey; by var1; run;

%end;
于 2015-10-02T12:39:20.157 回答