【问题标题】:How can I transform this code into macro?如何将此代码转换为宏?
【发布时间】:2020-09-21 16:06:04
【问题描述】:

所以我想做一个与 proc sql 和数据步骤混合的宏代码。我在 SAS 中有以下代码:

data work.calendar;
set work.calendar;
if business_day_count^=-1 then do;
  num_seq + 1;
  drop num_seq;
  business_day_count = num_seq;
end;
else
  business_day_count = -1;
run;

我想把它放到宏代码中,但它不起作用。

我的宏代码:

%macro1();    
data work.job_calendar;
    set work.job_calendar;
    %if business_day_count^=-1 %then %do;
      num_seq + 1;
      drop num_seq;
      business_day_count = num_seq;
    %end;
    else
      business_day_count = -1;
    run;
%mend;

整个代码是:

%macro update_day(date);

proc sql;
update work.job_calendar
        set business_day_count =
        case when datepart(calendar_date) = "&date"d then -1
        else business_day_count
        end;    
quit;
proc sql;
update work.job_calendar
        set status_ind =
        case when business_day_count = -1 then 'N'
        else status_ind
        end;    
quit;
proc sql;
update work.job_calendar
        set rundate_ind =
        case when business_day_count = -1 then 'N'
        else status_ind
        end;    
quit;
proc sql;
update work.job_calendar
        set daily_rundate_ind =
        case when business_day_count = -1 then 'N'
        else status_ind
        end;    
quit;
proc sql;
update work.job_calendar
        set weekly_rundate_ind =
        case when business_day_count = -1 then 'N'
        else status_ind
        end;    
quit;
proc sql;
update work.job_calendar
        set monthly_rundate_ind =
        case when business_day_count = -1 then 'N'
        else status_ind
        end;    
quit;

data work.job_calendar;
set work.job_calendar;
if business_day_count^=-1 then do;
  num_seq + 1;
  drop num_seq;
  business_day_count = num_seq;
end;
else
  business_day_count = -1;
%mend;

错误代码为:ERROR 180 - 322 语句无效或使用顺序不正确。我不知道我做错了什么。

【问题讨论】:

  • 您的代码不会尝试概括任何内容,因此不需要宏编码。您想在这里做什么或测试什么?
  • 它是更大代码的一部分。问题是这部分不起作用。 :S
  • 您没有显示如何调用宏以及传递了什么参数。我建议按照我链接的教程来转换您的代码。
  • 您的宏调用应该是:%update_day(04May2019); 调试选项是:options mprint symbolgen; 然后重新运行您的宏代码以获取错误。
  • 我的宏调用是:%update_day(04MAY2019)。有趣的是它在没有宏上下文的情况下工作,所以我很确定我做错了什么。

标签: sas sas-macro


【解决方案1】:

您正在混合数据步骤和宏代码。不确定您要达到什么目的,因此不知道要提出什么建议,但删除 %IF/%THEN 将使您的代码正常工作。

%macro1();    
data work.job_calendar;
    set work.job_calendar;
    if business_day_count^=-1 then do;
      num_seq + 1;
      drop num_seq;
      business_day_count = num_seq;
    end;
    else
      business_day_count = -1;
    run;
%mend;

%macro1;

这是converting working code to a macro 的教程和overall macro programming 的教程。

【讨论】:

    【解决方案2】:

    要定义宏,您需要使用 %MACRO 语句。另外,为什么将第 3 行和第 7 行从数据语句更改为宏语句?

    该代码无法运行。首先,%IF 始终为真,因为字符串business_day_count 永远不会匹配字符串-1。其次,您有一个 else 声明,而之前没有任何 if 声明。

    试试这样吧。

    %macro macro1();    
    data work.job_calendar;
      set work.job_calendar;
      if business_day_count^=-1 then do;
        num_seq + 1;
        drop num_seq;
        business_day_count = num_seq;
      end;
      else business_day_count = -1;
    run;
    %mend macro1;
    

    【讨论】:

    • 我解决了这个问题。这是代码的顺序。
    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2019-05-26
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多