¿Cómo puedo transformar este código en macro?

Sep 21 2020

Así que me gustaría hacer un código macro mezclado con proc sql y data step. Tengo el siguiente código en 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;

Me gustaría ponerlo en código macro, pero no funciona.

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

El código completo es:

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

El código de error es: ERROR 180 - 322 La declaración no es válida o se usa fuera de orden. No sé qué estoy haciendo mal.

Respuestas

1 Reeza Sep 21 2020 at 16:38

Está mezclando paso de datos y código de macro. No estoy seguro de lo que está tratando de lograr, por lo que no tengo idea de qué proponer, pero eliminar% IF /% THEN permitirá que su 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;

Aquí hay un tutorial sobre cómo convertir código de trabajo en una macro y otro sobre programación de macros en general .

Tom Sep 21 2020 at 21:39

Para definir una macro, debe utilizar la instrucción% MACRO. Además, ¿por qué cambió las líneas 3 y 7 de declaraciones de datos a declaraciones macro?

Ese código no puede funcionar. Primero,% IF siempre es verdadero ya que la cadena business_day_countnunca va a coincidir con la cadena -1. En segundo lugar, tiene una elsedeclaración sin ninguna ifdeclaración previa .

Intente algo como esto en su lugar.

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