version 9.0
#delimit;
set more off;
  quietly log;
  local logon = r(status);
  if "`logon'" == "on" {; log close; };
log using boehmke2009jop-mkdata, replace text;

/*	****************************************************************	*/
/*     	File Name:	boehmke2009jop-mkdata.do				*/
/*     	Date:   	June 08, 2010 						*/
/*      Author: 	Frederick J. Boehmke					*/
/*      Purpose:	This file creates the data set for the analysis of  	*/
/*			the directed dyad approach to state policy adoption	*/
/* 			using end of life pain-management data.			*/
/*      Input File:	boehmke2009jop.csv					*/
/*      Output File:	boehmke2009jop-mkdata.log, 				*/
/*			boehmke2009jop.dta, 					*/
/*			boehmke2009jop-bycomponent.dta 				*/
/*	Requires:	nbrs-code-dyads.ado.					*/
/*	Version:	Stata 9 or above.					*/
/*	****************************************************************	*/


	/********************************************************/
	/* This program takes the original state data set and 	*/
	/* creates the directed dyad data set.			*/
	/********************************************************/

	
program drop _all;
	
program define mkdyads;

  	foreach var of varlist _all {;

	  rename `var' `var'_02;

	  };

  	rename year_02 year;
  	rename time_02 time;
	rename fsmbgde_02 fsmbgde;
	rename time_prefsmb_02 time_prefsmb;
	rename time_pstfsmb_02 time_pstfsmb;

  	sort year stateno_02;

  	save boehmke2009jop-mkdyad, replace;

  use boehmke2009jop, clear;

	expand 50;

  	foreach var of varlist _all {;

	  rename `var' `var'_01;

	  };

  	rename year_01 year;
  	rename time_01 time;
	rename fsmbgde_01 fsmbgde;
	rename time_prefsmb_01 time_prefsmb;
	rename time_pstfsmb_01 time_pstfsmb;

  	sort year stateno_01;
  	generate stateno_02 = 1;

  quietly by year stateno_01: replace stateno_02 = stateno_02[_n-1] + 1 
	if stateno_01==stateno_01[_n-1] & year==year[_n-1];

  sort year stateno_02;

  joinby year stateno_02 using boehmke2009jop-mkdyad;

  save boehmke2009jop, replace;
  
  erase boehmke2009jop-mkdyad.dta;

end;


	/********************************************************/
	/* Open the monadic data set and create the dyadic data.*/
	/********************************************************/


insheet using boehmke2009jop.csv, clear;

		/* Create a few variables. */

  generat time = year - 1989;
  replace time = . if year < 1989;

  tsset stateno year;
  
  generat time_prefsmb = time*(year <  1999);
  generat time_pstfsmb = time*(year >= 1999);

  egen     prov_num = rowtotal(prov2-prov8);

  by stateno: generate prov_tot = sum(prov_num);
  generate prov_any = (prov_num >= 1);

		/* Did the state adopt each provision by the previous year? */
  
  forvalues num=2/8 {;

  	generat prov_lag_`num' = 0;
  	replace prov_lag_`num' = 1 if l.prov`num' == 1 & stateno==l.stateno;
  	replace prov_lag_`num' = 1 if l.prov_lag_`num' == 1 & stateno==l.stateno;

	};

		/* Did the state adopt each provision by the current year? */
  
  forvalues num=2/8 {;

  	generat prov_has_`num' = 0;
  	replace prov_has_`num' = 1 if prov`num' == 1;
  	by stateno: replace prov_has_`num' = l.prov_has_`num' if l.prov_has_`num' == 1;
	
	};

		/* Call the program to turn the monadic data into dyadic data. */
	
  save boehmke2009jop, replace;

  mkdyads;

  
	/****************************************************************/
	/* Create dyad-level variables including emulation variables.	*/
	/****************************************************************/
	
  
		/* Create dyad id variable. */
  
  egen dyad = group(state_01 state_02);

  tsset dyad year;

		/* How many components does state 01 not currently have */
		/* the same values for as state 02 had last year? 	*/

  generat emul_diff = 
	(prov_has_2_01 != prov_lag_2_02) + (prov_has_3_01 != prov_lag_3_02) + 
	(prov_has_4_01 != prov_lag_4_02) + (prov_has_5_01 != prov_lag_5_02) + 
	(prov_has_6_01 != prov_lag_6_02) + (prov_has_7_01 != prov_lag_7_02) + 
	(prov_has_8_01 != prov_lag_8_02) if year>1989;

		/* How many components did state 01 not have   	*/
		/* the same values as state 02 had last year? 	*/

  generat emul_diff_lag = 
	(prov_lag_2_01 != prov_lag_2_02) + (prov_lag_3_01 != prov_lag_3_02) + 
	(prov_lag_4_01 != prov_lag_4_02) + (prov_lag_5_01 != prov_lag_5_02) + 
	(prov_lag_6_01 != prov_lag_6_02) + (prov_lag_7_01 != prov_lag_7_02) + 
	(prov_lag_8_01 != prov_lag_8_02) if year>1989;

		/* Did the number of components with different values  	*/
		/* decrease? If so, then state 01 emulated state 02. 	*/
		/* Then remove observations with all 7 adopted. 	*/
		
  by dyad: generat emul_net = (abs(emul_diff) < abs(emul_diff_lag)) if year>1989;
  replace  emul_net = . if l.prov_tot_01==7;

		/* Did state 02 have any component that state 01 did 	*/
		/* not last year? If so, then state 01 has the 		*/
		/* opportunity to emulate state 02.			*/
  
  generat emul_opp_net = 
	(prov_lag_2_01 == 0 & prov_lag_2_02 == 1) + (prov_lag_3_01 == 0 & prov_lag_3_02 == 1) + 
	(prov_lag_4_01 == 0 & prov_lag_4_02 == 1) + (prov_lag_5_01 == 0 & prov_lag_5_02 == 1) + 
	(prov_lag_6_01 == 0 & prov_lag_6_02 == 1) + (prov_lag_7_01 == 0 & prov_lag_7_02 == 1) + 
	(prov_lag_8_01 == 0 & prov_lag_8_02 == 1) if year>1989;

	recode emul_opp_net 1/max=1;
	
  keep mb* rpcpinc* ideology* prov* emul* fsmb* year* state* time* dyad;


	/********************************************************/
	/* Create the relative variables measures. 		*/
	/********************************************************/


  foreach var of newlist rpcpinc ideology mblegal mbautho mbnonmd {;

	generat `var'_diff = abs(`var'_01 - `var'_02);

	};


	/********************************************************/
	/* Create the neighbors variable and associated 	*/
	/* measures of adoption. 				*/
	/********************************************************/

		/* This program creates an indicator for 	*/
		/* whether two states are contiguous. 		*/

  do nbrs-code-dyads.do;

		/* Then multiply whether state 02 has the  	*/
		/* component by whether state 02 is a neighbor. */
  
  forvalues provno=2/8 {;

  	generat nbr_lag_`provno' = nbr*prov_lag_`provno'_02;
  	egen    nbrs_lag_`provno' = total(nbr_lag_`provno'), by(stateno_01 year);

  	};

  generat nbr_lag_any = max(nbr_lag_2, nbr_lag_3, nbr_lag_4, nbr_lag_5, nbr_lag_6, nbr_lag_7, nbr_lag_8);
  egen    nbr_lag_sum = rowtotal(nbr_lag_2 nbr_lag_3 nbr_lag_4 nbr_lag_5 nbr_lag_6 nbr_lag_7 nbr_lag_8);

  egen    nbrs_lag_any = total(nbr_lag_any), by(stateno_01 year);
  egen    nbrs_lag_sum = total(nbr_lag_sum), by(stateno_01 year);


	/********************************************************/
	/* Drop intermediary variables for each component.	*/
	/********************************************************/

	
drop 	prov_lag_2_01-prov_has_8_01 
	prov_lag_2_02-prov_has_8_02 
	prov2_01-prov8_01 
	prov2_02-prov8_02  
	nbr_lag_2-nbrs_lag_8
	emul_diff emul_diff_lag;
  
  
	/********************************************************/
	/* Label variables and data set.			*/
	/********************************************************/


  label data "Dyadic data set with state pain management emulations (F. Boehmke)";

	label variable mblegal_01   	"MB Legal Counsel - State 01";
	label variable mblegal_02   	"MB Legal Counsel - State 02";
	label variable mblegal_diff 	"MB Legal Counsel - Difference";

	label variable mbnonmd_01   	"MB Non-M.D. Members - State 01";
	label variable mbnonmd_02   	"MB Non-M.D. Members - State 02";
	label variable mbnonmd_diff  	"MB Non-M.D. Members - Difference";

	label variable mbautho_01    	"MB Authorization - State 01";
	label variable mbautho_02    	"MB Authorization - State 02";
	label variable mbautho_diff  	"MB Authorization - Difference";

	label variable rpcpinc_01   	"Income - State 01";
	label variable rpcpinc_02   	"Income - State 02";
	label variable rpcpinc_diff 	"Income - Difference";

	label variable ideology_01  	"Liberal Ideology - State 01";
	label variable ideology_02  	"Liberal Ideology - State 02";
	label variable ideology_diff	"Liberal Ideology - Difference";

	label variable nbr_lag_sum 	"Total Provisions - State 02 Contiguous";
	label variable nbrs_lag_sum 	"Total Provisions - Contiguous States";
	label variable prov_tot_02 	"Total Provisions - State 02";

	order dyad year state_01 stateno_01 statenam_01 state_02 stateno_02 statenam_02 
	  emul* *_01 *_02 *_diff nbr*;
	
	compress;

save boehmke2009jop, replace;

log close;
clear;
exit, STATA;		