##### BEGIN CODE ######



#!/usr/bin/perl
######
#
# --start, -s = The date you would like to start generating regressors
#--end, -e = When to stop generating holiday regressros
# --scope, -c = D, W for Daily or Weekly respectively (e.g. Does this week
have a particular holiday)
# --file, -f = Ummm where to write the output silly!
#
# **NOTE** The EOM holiday is "End of Month" for computer systems this may
be important for
# extra processing and what not.
#
# You may need to set yout TZ environment variable if the script cannot
# determine your time zone from the system (e.g. SET TZ=CST )
######
use Getopt::Long;
use Date::Manip;
use Spreadsheet::WriteExcel;
use Calendar::Functions;
use Date::Holidays::USFederal;
use Set::Array;
use POSIX qw/strftime/;
use Time::Local;

my @regressors = ();
#my $holidays = Date::Holidays->new(countrycode => 'us');

$result = GetOptions ("start|s=s" => \$start,
   "end|e=s" => \$end,
   "scope|c=s" => \$scope,
   "file|f=s" => \$filename);




print "Generating Holiday Dummy Variables starting $start to $end generated
by $scope. Output to $filename \n";

#print all the dates based on scope as a test




$startDate=ParseDate(\$start);
if (! $startDate) {
    print "Error in the date";exit;
}
$endDate= ParseDate($end);
print "Start Date: ",UnixDate($startDate,"%m/%d/%Y"),"\n";
print "End Date: ",$end,"\n";

print "Last Day in Month: ",UnixDate(ParseDate("last day in JAN
2004"),"%m/%d/%Y"),"\n";



#HEADER OUTpUT
print
"Date,HLY-NewYear,HLY-MLK,HLY-PRES,HLY-MEMORIAL,HLY-J4,HLY-LABOR,HLY-COLUMBUS,HLY-VETS,HLY-THANKS,HLY-XMAS,HLY-ELECT,HLY-PATRIOT,EOM\n";
$baseDate=$startDate;

if ($scope eq "d"){

while(Date_Cmp($baseDate,$endDate)<0)
{
print UnixDate($baseDate,"%m/%d/%Y"), ",";
if(holidayCheck($baseDate) eq "New Year's Day"){print "1,"} else {print
"0,"};
if(holidayCheck($baseDate) eq "Martin Luther King, Jr. Day"){print "1,"}
else {print "0,"};
if(holidayCheck($baseDate) eq "Presidents' Day"){print "1,"} else {print
"0,"};
if(holidayCheck($baseDate) eq "Memorial Day"){print "1,"} else {print "0,"};
if(holidayCheck($baseDate) eq "Independence Day"){print "1,"} else {print
"0,"};
if(holidayCheck($baseDate) eq "Labor Day"){print "1,"} else {print "0,"};
if(holidayCheck($baseDate) eq "Columbus Day"){print "1,"} else {print "0,"};
if(holidayCheck($baseDate) eq "Veterans' Day"){print "1,"} else {print
"0,"};
if(holidayCheck($baseDate) eq "Thanksgiving Day"){print "1,"} else {print
"0,"};
if(holidayCheck($baseDate) eq "Christmas Day"){print "1,"} else {print
"0,"};
if(holidayCheck($baseDate) eq "Election Day"){print "1,"} else {print "0,"};
if(holidayCheck($baseDate) eq "U.S. Patriot Day Unofficial
Observation"){print "1,"} else {print "0,"};
if(holidayCheck($baseDate) eq "EOM"){print "1"} else {print "0"};
print "\n";

$baseDate=DateCalc($baseDate,"+1 day");
}

} # END IF D

if($scope eq "w") {

while(Date_Cmp($baseDate,DateCalc($endDate,"+7 days"))<0)
{
print UnixDate($baseDate,"%m/%d/%Y"), ",";

if(

(holidayCheck(DateCalc($baseDate,"+0 days")) eq "New Year's Day") ||
(holidayCheck(DateCalc($baseDate,"+1 days")) eq "New Year's Day") ||
(holidayCheck(DateCalc($baseDate,"+2 days")) eq "New Year's Day") ||
(holidayCheck(DateCalc($baseDate,"+3 days")) eq "New Year's Day") ||
(holidayCheck(DateCalc($baseDate,"+4 days")) eq "New Year's Day") ||
(holidayCheck(DateCalc($baseDate,"+5 days")) eq "New Year's Day") ||
(holidayCheck(DateCalc($baseDate,"+6 days")) eq "New Year's Day")

){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Martin Luther King, Jr. Day"
||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Martin Luther King, Jr. Day"
||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Martin Luther King, Jr. Day"
||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Martin Luther King, Jr. Day"
||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Martin Luther King, Jr. Day"
||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Martin Luther King, Jr. Day"
||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Martin Luther King, Jr. Day"

){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Presidents' Day" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Presidents' Day" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Presidents' Day" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Presidents' Day" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Presidents' Day" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Presidents' Day" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Presidents' Day"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Memorial Day" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Memorial Day" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Memorial Day" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Memorial Day" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Memorial Day" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Memorial Day" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Memorial Day"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Independence Day" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Independence Day" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Independence Day" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Independence Day" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Independence Day" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Independence Day" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Independence Day"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Labor Day" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Labor Day" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Labor Day" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Labor Day" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Labor Day" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Labor Day" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Labor Day"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Columbus Day" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Columbus Day" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Columbus Day" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Columbus Day" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Columbus Day" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Columbus Day" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Columbus Day"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Veterans' Day" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Veterans' Day" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Veterans' Day" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Veterans' Day" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Veterans' Day" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Veterans' Day" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Veterans' Day"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Thanksgiving Day" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Thanksgiving Day" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Thanksgiving Day" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Thanksgiving Day" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Thanksgiving Day" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Thanksgiving Day" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Thanksgiving Day"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Christmas Day" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Christmas Day" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Christmas Day" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Christmas Day" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Christmas Day" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Christmas Day" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Christmas Day"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "Election Day" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "Election Day" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "Election Day" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "Election Day" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "Election Day" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "Election Day" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "Election Day"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "U.S. Patriot Day Unofficial
Observation" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "U.S. Patriot Day Unofficial
Observation" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "U.S. Patriot Day Unofficial
Observation" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "U.S. Patriot Day Unofficial
Observation" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "U.S. Patriot Day Unofficial
Observation" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "U.S. Patriot Day Unofficial
Observation" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "U.S. Patriot Day Unofficial
Observation"
){print "1,"} else {print "0,"};

if(
holidayCheck(DateCalc($baseDate,"+0 days")) eq "EOM" ||
holidayCheck(DateCalc($baseDate,"+1 days")) eq "EOM" ||
holidayCheck(DateCalc($baseDate,"+2 days")) eq "EOM" ||
holidayCheck(DateCalc($baseDate,"+3 days")) eq "EOM" ||
holidayCheck(DateCalc($baseDate,"+4 days")) eq "EOM" ||
holidayCheck(DateCalc($baseDate,"+5 days")) eq "EOM" ||
holidayCheck(DateCalc($baseDate,"+6 days")) eq "EOM"
){print "1,"} else {print "0,"};

print "\n";

$baseDate=DateCalc($baseDate,"+7 days");

} #end while

} #end if w






















sub holidayCheck {
my $t = shift;
my $y = UnixDate($t,"%Y");
my $m = UnixDate($t,"%m");
my $B = UnixDate($t,"%B");
my $d = UnixDate($t,"%d");

my $checkDate = "$m/$d/$y";
# YOU CaN ADD MOrE HOLIDAYs HERE BUT YoU nEED TO ADD COLUMNs TO THE HEADER
# AND ADDITIONAL "if(holidayCheck..." LINES

SWITCH: {

$checkDate eq "01/01/$y" and do {return "New Year's Day";};
$checkDate eq UnixDate(ParseDate("3rd monday of january $y"),"%m/%d/%Y") and
do {return "Martin Luther King, Jr. Day";};
$checkDate eq UnixDate(ParseDate("3rd monday of february $y"),"%m/%d/%Y")
and do {return "Presidents' Day";};
$checkDate eq UnixDate(ParseDate("last monday of may $y"),"%m/%d/%Y") and do
{return "Memorial Day";};
$checkDate eq "07/04/$y" and do {return "Independence Day";};
$checkDate eq UnixDate(ParseDate("first monday of september $y"),"%m/%d/%Y")
and do {return "Labor Day";};
$checkDate eq UnixDate(ParseDate("second monday of october $y"),"%m/%d/%Y")
and do {return "Columbus Day";};
$checkDate eq "11/11/$y" and do {return "Veterans' Day";};
$checkDate eq UnixDate(ParseDate("fourth thursday of november
$y"),"%m/%d/%Y") and do {return "Thanksgiving Day";};
$checkDate eq "12/25/$y" and do {return "Christmas Day";};
$checkDate eq UnixDate(ParseDate("first tuesday of november $y"),"%m/%d/%Y")
and do {return "Election Day";};
$checkDate eq "09/11/$y" and do {return "U.S. Patriot Day Unofficial
Observation";};
$checkDate eq UnixDate(ParseDate("last day in $B $y"),"%m/%d/%Y") and do
{return "EOM";};

} #END SELECT


}

sub first_sunday_of_calmonth {
  my $m = shift;
  my $y = shift;

  my $dow = strftime("%w", 0,0,0,1,$m-1,$y-1900);
  my $time = timelocal(0,0,0,1,$m-1,$y-1900);
  return $time - ($dow * 24 * 60 * 60);
}
####### END CODE ########

I use this to create dummy regressors for US holidays that I throw in a list
of regressors. I normally work with weekly data but this will generate daily
entries also. Enjoy and that you to the R-Help list. If you are not a perl
user this will more then likely be a bunch of jibberish. Please see the
"use" lines for needed CPAN modules.

	[[alternative HTML version deleted]]


