* ----------------------------------------------------------------
* ----------------------------------------------------------------
*
* PROJECT: ESTIMATING SICK REPORTING AT OS-CALAGARY 
*          SAT 13 FEB-SUN 28 FEB
*          WEEKNR=7,8 OS 
* 
* DATA:    LINDA 1988 WITH SICK REGISTER
*
*          BASIC AMOUNT 1988 = 25800
* ----------------------------------------------------------------
* ----------------------------------------------------------------
clear
use li88  			      /* Linda matched with sick register */
keep if burvkod==1 		/* Keep the smaple individual */

set more off

* ----------------------------------------------------------------
* ----------------------------------------------------------------
* VARIABLE MANAGEMENT 
* ----------------------------------------------------------------
* ----------------------------------------------------------------

* ----------------------------------------------------------------
* DATES --- 15feb1987=10272, Monday
* ----------------------------------------------------------------
*gen edate=date(startd,"ymd")
gen xfromread=xfrom
format xfromread %d
gen weekday = dow(xfrom)

*order xfrom xfromread

drop startd slutd xtom regar  /* Drop unnecessary variables */

* ----------------------------------------------------------------
* DEFINE PERIODS --- CALGARY PERIOD = 1
* ----------------------------------------------------------------
gen period=1 if xfrom>=10272 & xfrom<10272+14      /* 14-day period. Mond-Sunday */

program drop _all
*************** PROGRAM STARTS HERE ******************************
program define percr            /* PERIOD CREATER PROGRAM */
  local i=1
    while `i'<=23 {

	replace period = (`i'+1) if ( xfrom>=10272+(14*`i') & xfrom<10272+(14*`i')+14 )
	replace period = (1-`i') if ( xfrom>=10272-14*`i'   & xfrom<10272-(14*(`i'-1)) )

  local i=`i'+1
  }

end
percr
*********** PROGRAM ENDS HERE *************************************

* ----------------------------------------------------------------
* BACKGROUND VARIABLES
* ----------------------------------------------------------------
gen age=alder

gen man=1 if bkon==1 
replace man=0 if bkon==2

gen marr=1 if civ==2 | civ==3 | civ==7
replace marr=0 if civ==1 | civ==4 | civ==5 | civ==6 | civ==8 | civ==9
replace marr=0 if marr==.

gen numbchild=barny+barnae

replace kulon1=0 if kulon==.

gen inc=kulon1*100
gen comprate=0.9 if inc<=7.5*25800
replace comprate=0.9*7.5*25000/inc if inc>7.5*25800

gen overcap=1 if inc>7.5*25800
replace overcap=0 if inc<=7.5*25800

gen selfe=1 if ystu==1      /* Self-employed */
replace selfe=0 if ystu==0  /* Employed */

gen sni=int(kusni/10000)
replace sni=0 if sni==.

gen sec=int(kuinst/1000)
gen sector=1 if sec==1  
replace sector=2 if sec>=2 & sec<=6
replace sector=3 if sec==. | sec==0

* ----------------------------------------------------------------
* ----------------------------------------------------------------
* LIMIT THE SAMPLE
* ----------------------------------------------------------------
* ----------------------------------------------------------------
keep if age>=20 & age<=64
drop if selfe==.

keep bidnr xfrom xfromread period length age man marr numbchild inc selfe comprate overcap sni sector lan

* ----------------------------------------------------------------
* ----------------------------------------------------------------
* PPREPARE RESHAPING DATA
* ----------------------------------------------------------------
* ----------------------------------------------------------------
replace xfrom=0 if xfrom==.
replace xfromread=0 if xfromread==.
replace length=0 if length==.
replace period=-999 if period==.

sort bidnr xfrom
by bidnr: gen x=_n

reshape wide xfrom xfromread length period, i(bidnr) j(x) 
/* j=1,...,K where K important for comming programming */

compress 

save calg88, replace

* ----------------------------------------------------------------
* ----------------------------------------------------------------
* PROGRAM SICK REPORTING 1-Z DAYS WITHIN 2-WEEK PERIODS
* THE peri_Z data can then be appended 
* ----------------------------------------------------------------
* ----------------------------------------------------------------
 
* z=1,...,14 (or Z) lengths, 
* X=1,...,43 (or X) number of spells, 
* i=-1,0,1,...,23 (or K) number of periods 

program drop _all
****************** PROGRAM STARTS HERE **************************
program define sickcr  /* SICK CREATER */
  local i=0            /* i=1,...,K specify number of periods */
    while `i'<=1 {

	clear
	use calg88
	  gen per=`i'
	  gen sick=0

		forvalues z=1/14 {
		  for num 1/43: replace sick=1 if periodX==`i' & lengthX==`z' & (xfromX<10272-`z'+1+`i'*14)
		}

	 drop length* xfrom* xfromread* period*
	 save per`i'_14, replace

  local i=`i'+1
}
end 
sickcr
******************* PROGRAM ENDS HERE ****************************
