简体   繁体   English

SAS:数据步骤中的宏过程错误

[英]SAS: macro procedure error in data step

I cannot find the solution to this error. 我找不到该错误的解决方案。 I tried usign %eval, %sysfunc, and %sysevalf, with no success. 我尝试了成功的%eval,%sysfunc和%sysevalf,但没有成功。 What is needed to correctly evaluate the "&set" in the macro? 正确评估宏中的“&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.

The logic behind you're macro is wrong. 您是宏背后的逻辑是错误的。

You are calling that macro inside a datastep, so that macro should contain something (a statement) that could be resolved inside the datastep. 您正在数据步骤内调用该宏,因此该宏应包含可以在数据步骤内解析的内容(一条语句)。

If-else won't be written in macro language but in normal datastep language. If-else不会以宏语言编写,而是以普通的datastep语言编写。

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

now if you use your call you will have your datastep resolved this way: 现在,如果您使用呼叫,则可以通过以下方式解决数据步骤:

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

will became: 将成为:

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

On your code you were using macro code so your step were resolved internally to the macro boolean statement %if &set/100 resolved were %if setcon/100 where setcon was a string (here we are conditionally speaking with macro language, writing the name of the variable won't catch the value of the variable 'cause it is completelly independent from the datastep). 在您的代码上,您使用的是宏代码,因此您的步骤在内部由宏布尔语句%if和&set / 100解析,而%if setcon / 100则setcon是字符串(此处,我们有条件地使用宏语言,写为变量将无法捕获变量的值,因为它与数据步骤完全独立)。

You should think to macro language only to something that could write down code for you, for example something like your first try could be used to insert a statement conditionally to the value of a macro variable like: 您应该仅将宏语言视为可以为您编写代码的内容,例如,可以使用诸如您的第一次尝试之类的条件将语句有条件地插入到宏变量的值中,例如:

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;

But you can use macro code outside from the datastep too, for example to execute a datastep or something else conditionally to the value of a macro variable: 但是您也可以在数据步骤之外使用宏代码,例如对宏变量的值有条件地执行数据步骤或其他操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM