0
votes

So I'd like to do a macro code mixed with proc sql and data step. I have the following code 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;

I'd like to put it into macro code, but it doesn't work.

My macro code:

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

The whole code is:

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

The error code is: ERROR 180 - 322 Statement is not valid or it is used out of proper order. I don't know what I'm doing wrong.

2
Your code doesn't attempt to generalize anything so macro coding isn't required. What are you trying to do or test here?Reeza
It is part of a bigger code. The problem is that this part isn't working. :Scraze
You didn't show how you called the macro and what parameter you passed. I would suggest following the tutorial I linked to convert your code.Reeza
Your macro call should be : %update_day(04May2019); Debugging options are: options mprint symbolgen; then re-run your macro code to get the errors.Reeza
My macro call is: %update_day(04MAY2019). The interesting thing is that it's working without the macro context so I'm pretty sure that I'm doing something wrong.craze

2 Answers

1
votes

You're mixing data step and macro code. Not sure what you're trying to achieve so no idea on what to propose but removing the %IF/%THEN will allow your code to work.

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

Here is a tutorial on converting working code to a macro and one on overall macro programming.

0
votes

To define a macro you need to use the %MACRO statement. Also why did you change lines 3 and 7 from data statements to macro statements?

enter image description here

That code cannot work. First the %IF is always true since the string business_day_count is never going to match the string -1. Second you have an else statement without any previous if statement.

Try something like this instead.

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