0

I'm relatively new to SAS and very new to Macros, so please forgive me if this is a basic question, but I haven't been able to find answers elsewhere that have worked for me. Basically, I have a SAS program that runs every weekday, but I need it to run a different series of steps on regular business days than it does on company holidays. I have an organizational dataset that includes the date and an indicator variable if that date is a company holiday (1 = yes, 0 = no). In terms of logic, I want the code to do the following:

%LET Holiday_Macro

%If &Holiday_Macro = 1 %THEN %DO;
/*Insert holiday program here*/

%ELSE %DO;
/*Insert business day program here*/

But I'm struggling to define "Holiday_Macro" appropriately. In effect, it would need to take the day's date and the holiday indicator into account, but I haven't been able to figure out how to do that. Any input would be greatly appreciated!

1
  • Can you share what you tried and explain how it failed? (Note your %LET is missing the ending semicolon.) For detailed help you need to provide details of your company holiday dataset. Commented May 15 at 12:53

2 Answers 2

3

You can create your HOLIDAY_MACRO macro variable by running a simple data step against your company holiday dataset.

So if your dataset is named HOLIDAYS and the variable with the date of the holiday is named DATE then the code might look like:

%let holiday_macro=0;
data _null_;
  set holidays;
  where date = today();
  call symputx('holiday_macro','1');
run;
Sign up to request clarification or add additional context in comments.

2 Comments

I think this is what I'm looking for but want to clarify. If I understand this correctly, the %LET statement is setting the holiday_macro = 0 by default, and when the today's date is a holiday, then the WHERE and CALL SYMPUTX statements set the holiday_macro = 1 for that day only. Is that correct? If so, then my next step would be to write the rest of the program to do something along the lines of: %IF holiday_macro = 1 %THEN %DO [insert holiday program]; %ELSE DO [insert non-holiday program]; But please let me know if I'm misinterpreting. Either way, very helpful! Thank you!
Yes. The posted code is just to create the macro variable. You need to figure out how to use it. Note there is no need to make an actual macro as long as the conditional logic you need is limited to the %if/%then/%do (and %else/%do) logic in your example. If you need to use more complex conditional logic like an iterative %do statement you will need to wrap the code into macro definition (%macro ... %mend) and then call the macro to have it execute.
2

You can have two versions of the program code in separate files.

  • myProgram_Holiday.sas

  • myProgram_Regular.sas

Then you would %include the program depending on the holiday state.
(Edit added today() check)

%let program_name = myProgram_Regular.sas ;
* Assume organization_data table contains columns "date" and "isHoliday" ;
proc sql noprint ;
  select 'myProgram_Holiday.sas'
  into :programName
  from company.organizational_data
  where company.date = today() and isHoliday
  ;
quit ;

filename programs "<path to programs folder>" ;
%include programs("&programName") / source2 ;

2 Comments

Interesting. So just to make sure I understand what is happening here (as a new-ish SAS user), I write a program that essentially has two separate sas files on standby (holiday.sas and nonholiday.sas), and, using the %include statement, tells SAS to run "holiday.sas" if today is a holiday, and run "nonholiday.sas" otherwise. Is that correct?
Correct. However, when the variation between the two programs is very minor you can often manage the variation within a single program using macro variables for the 'abstraction' or boilerplating. SAS novices can jump into macro coding too soon, ymmv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.