如何将此代码转换为宏?

How can I transform this code into macro?

所以我想编写一个混合了 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 语句无效或使用顺序不正确。我不知道我做错了什么。

您正在混合使用数据步骤和宏代码。不确定您要实现的目标,因此不知道要提出什么建议,但是删除 %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 and one on overall macro programming 的教程。

要定义宏,您需要使用 %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;