Como posso transformar esse código em macro?

Sep 21 2020

Então, eu gostaria de fazer um código de macro misturado com proc sql e etapa de dados. Eu tenho o seguinte código no 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;

Eu gostaria de colocá-lo em código de macro, mas não funciona.

Meu código 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;

Todo o código é:

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

O código de erro é: ERROR 180 - 322 A instrução não é válida ou foi usada fora da ordem. Não sei o que estou fazendo de errado.

Respostas

1 Reeza Sep 21 2020 at 16:38

Você está misturando etapa de dados e código de macro. Não tenho certeza do que você está tentando alcançar, então nenhuma ideia sobre o que propor, mas remover o% IF /% THEN permitirá que seu código funcione.

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

Aqui está um tutorial sobre como converter o código de trabalho em uma macro e outro sobre a programação geral de macro .

Tom Sep 21 2020 at 21:39

Para definir uma macro, você precisa usar a instrução% MACRO. Além disso, por que você mudou as linhas 3 e 7 de declarações de dados para declarações macro?

Esse código não pode funcionar. Primeiro, o% IF é sempre verdadeiro, pois a string business_day_countnunca vai corresponder à string -1. Em segundo lugar, você tem uma elsedeclaração sem qualquer ifdeclaração anterior .

Em vez disso, tente algo assim.

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