【问题标题】:SAS: macro procedure error in data stepSAS:数据步骤中的宏程序错误
【发布时间】:2015-10-02 12:35:05
【问题描述】:

我找不到此错误的解决方案。我尝试了 ussign %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.

【问题讨论】:

    标签: macros sas datastep


    【解决方案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;
    

    但您也可以在数据步之外使用宏代码,例如,根据宏变量的值有条件地执行数据步或其他操作:

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

    【讨论】:

    • 很遗憾您的建议不起作用。我收到类似的错误:错误:在需要数字操作数的 %EVAL 函数或 %IF 条件中找到字符操作数。条件是:setcon/100 错误:宏 SET_ISTITUZIONALE 将停止执行
    • 这次宏变量集的值是setcon,所以你试图将“setcon”字符串划分为一个数字......请发布完整的代码,包括宏调用。
    • 不是,setcon是变量名(是分类号),是数字(检查不是注册为字符的数字)。
    • 好的,既然我们有了宏调用,我可以说你正在编写的宏背后的逻辑有什么问题,我将更新答案
    • 这就是我在答案开头写的,包含在宏一个数据步语句中。 (第一个密码框)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多