*! version 1.0.0 November 9, 1999 @ 15:13:44 *! finds the number of workdays between date1 and date2 program define workdays version 6.0 #delimit; syntax varlist (min=2 max=2 numeric) [if] [in] [using], GEN(str) [lazy] [holiday(str)]; #delimit cr if "`gen'"=="" { display in red "I need to generate a new variable!" exit 110 } else { confirm new var `gen' } if "`using'"=="" { if "`holiday'"!="" { display in red "The holiday option can be used only if the using option is used!" exit 198 } } else { if "`holiday'"=="" { display in red "The using option requires the holiday option!" exit 198 } } tempvar useme marksample useme tokenize `varlist' local early "`1'" local late "`2'" if `"`using'"'!=`""' { if "$S_HOLI"=="" | "`lazy'"=="" { display in blue "about to get rid of sholi" global S_HOLI preserve use `using', clear quietly describe local cnt 1 local max = _N while `cnt'<=`max' { local newdate = `holiday'[`cnt'] global S_HOLI "$S_HOLI `newdate'" local cnt = `cnt' + 1 } restore } } tempvar diff dearly dlate quietly { gen byte `dearly' = dow(`early') gen byte `dlate' = dow(`late') } /* the following formula is rather inefficient, I believe */ #delimit; gen long `diff' = 5*(int((`late'-`early')/7)-(`dlate'>=`dearly')-(`late'<`early')) + min(4, max(5-`dearly',0)) + min(5, `dlate') if `useme'; #delimit cr if "$S_HOLI"!="" { tokenize "$S_HOLI" local cnt 1 while "``cnt''"!="" { if dow(``cnt'')!=0 & dow(``cnt'')!=6 { quietly replace `diff' = `diff' - ((``cnt''>`early') & (``cnt''<=`late')) if `useme' } local cnt = `cnt' + 1 } } quietly compress `diff' label var `diff' "Work days between `early' and `late'" rename `diff' `gen' end