Come posso trasformare questo codice in macro?

Sep 21 2020

Quindi mi piacerebbe creare un codice macro misto a proc sql e data step. Ho il seguente codice in 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;

Mi piacerebbe inserirlo nel codice macro, ma non funziona.

Il mio codice 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;

L'intero codice è:

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

Il codice di errore è: ERRORE 180-322 L'istruzione non è valida o è utilizzata nell'ordine corretto. Non so cosa sto sbagliando.

Risposte

1 Reeza Sep 21 2020 at 16:38

Stai mescolando passaggio dati e codice macro. Non sei sicuro di cosa stai cercando di ottenere, quindi non ho idea di cosa proporre, ma rimuovere% IF /% THEN consentirà al tuo codice di funzionare.

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

Ecco un tutorial sulla conversione del codice funzionante in una macro e uno sulla programmazione generale delle macro .

Tom Sep 21 2020 at 21:39

Per definire una macro è necessario utilizzare l'istruzione% MACRO. Inoltre, perché hai modificato le righe 3 e 7 dalle istruzioni sui dati alle istruzioni macro?

Quel codice non può funzionare. Innanzitutto% IF è sempre vero poiché la stringa business_day_countnon corrisponderà mai alla stringa -1. Secondo, hai una elsedichiarazione senza alcuna ifdichiarazione precedente .

Prova invece qualcosa di simile.

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