(: file: dt08_189_econ.xq date: 07-Aug-2009 author: Gary Lewis purpose: Creates the XML with unemployment and recession data for the project addressing the question: Is higher education countercyclical? 1. Documentation is available at: http://garymlewis.com/instchg/public/xquery/dt08_189_doc.xq. Follow the steps in this documentation to Step 12. 2. Run the current program as follows (Step 12 in documentation): zorba -o dt08_189_econ.xml -f -q dt08_189_econ.xq -z indent=yes Note: This program pipelines through several temporary stages. :) { let $currDate := current-date(), (: set last known date for current recesion to current date :) $year := xs:string(year-from-date($currDate)), $mon := month-from-date($currDate), $month := if ($mon < 10) then concat('0',xs:string($mon)) else xs:string($mon), $day := xs:string(day-from-date($currDate)), $today := concat($year,'-',$month,'-',$day), $tmp1 := for $i in doc("fred_us_recessions.xml")/recessions/recession let $peak := $i/@peak, $trough := if ($i/@trough = "today") then $today else $i/@trough return return let $tmp2 := (: create a time series to hold unemployment and recession data :) for $yr in 1940 to 2010 for $mon in 1 to 12 let $year := xs:string($yr), $month := if ($mon < 10) then concat('0',xs:string($mon)) else xs:string($mon), $date := concat($year,'-',$month,'-','01') return return let $tmp3 := for $j in $tmp2 (: include unemployment rates in time series :) let $series_date := $j/@value, $m := doc("fred_UNRATE_observations.xml")/mfred_1series_observations/series/observation[@date = $series_date], $unemployment_rate := $m/@value return return let $tmp4 := for $k in $tmp3 (: include recession indicator in time series :) let $series_date := $k/@value, $recession_peak := max($tmp1[@peak <= $series_date]/xs:date(@peak)), $recession_trough := $tmp1[@peak = $recession_peak]/@trough, $recession_indicator := if ($series_date >= $recession_peak and $series_date <= $recession_trough) then "1" else "0" return return $tmp4 }