I have a SAS dataset called "holidays" that is structured as shown below that spans 1985-2023. I have dummy variables for the Friday and the weekend, and most importantly holidays.
I am trying to write a loop which works through a list of all months and days of the week to determine what percentage of a day of the week, for a given month, falls on a holiday. For instance, what percentage of Monday's in January have been a federal holiday between 1985 and 2023? What percentage of Tuesday's in January, etc... Below is my code thus far:
@sas_user_1001 wrote:
If I'm understanding correctly, you are asking why I have %put at the end for the binary variables? I am simply wanting to see that the code assigned the percentages as I intended; it's just a check. Those percentages get called up and used later on in the code.
Placing that much calculation result into macro variables is usually a poor idea.
How exactly are those macro values used later?
And the point is that the macro variables involved with your %put do no exist after the macro ends as they are local in scope with code as written.
Why are you trying to print the value of LOCAL macro variables after the macro has finished running?
@sas_user_1001 wrote:
If I'm understanding correctly, you are asking why I have %put at the end for the binary variables? I am simply wanting to see that the code assigned the percentages as I intended; it's just a check. Those percentages get called up and used later on in the code.
Placing that much calculation result into macro variables is usually a poor idea.
How exactly are those macro values used later?
And the point is that the macro variables involved with your %put do no exist after the macro ends as they are local in scope with code as written.
I wanted to circle back to this point. I was able to specify the variable names using symputx with 'G' to make these global variables. The code now works due to this insight.
Before you started down this macro path did you have code that worked for some period that did not involve any macro variables or coding?
If not the basic approach is to do such. Then see if a macro is even needed.
Is your HOLDUM variable supposed to be the indicator that the day is a holiday?
If so you may want to consider either of these codes:
proc tabulate data=holidays; class month dow; var holdum; table month, dow*holdum*mean*f=percent8.2; run; proc tabulate data=holidays; class month dow holdum; table month, dow*holdum*pctn<month> /misstext=' '; run;
Which uses the power of the procedure to do the grouping and counting.
Or
Proc freq data=holidays; tables month*dow*holdum/outpercent; ods output crosstabfreqs=myoutput; run;
though the output data set Myoutput would need a bit of massaging to get just the holidays. Look at the variable ROWPERCENT where HOLDUM=1.
I have no idea what your dummy variables mean or what role they play in this exercise. So you may discuss them.
If your process is modeling dates then you should be able to get one of the HOLIDAY functions with that to tell if the date is a holidy.
Assuming that HOLDUM is coded as 1/0 with 1 meaning HOLIDAY you can get your results using PROC SUMMARY. The MEAN of a 1/0 variable is the percent of times the value was 1.
proc summary data=holidays nway;
class month dow ;
var holdum;
output out=percent_holiday mean=percent_holiday;
run;
And you can also skip the macro variables since you could just use the dataset to drive your simulation.
Let's try it:
data holidays;
do date='01JAN1985'd to '31DEC2023'd ;
month=month(date);
dow=weekday(date);
holdum=0<holidaycount(date);
output;
end;
format date yymmdd10.;
run;
proc summary data=holidays nway;
class month dow ;
var holdum;
output out=percent_holiday mean=percent_holiday;
run;
If we check how often Thursday in November is a holiday we get just over 26% of the time.
11 | 5 | 3 | 167 | 0.26347 |
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.