program corr_create /* this command is called "corr_create" and creates a new variable that is a series of corr coefficients of two variables, `1' and `2', for different countries (cn `3') and years (year) */ /* `1' -- name of the variable `2' -- max of the variable for the base country */ /* keep in mind that this program assigns *zero* for missing variables (that arise if the correlation is "undefined") */ set more off use mm, clear sort cn year quietly by cn year: egen c=count(`1') quietly gen cd=0 quietly replace cd=1 if c~=0 quietly replace cd=. if c<=2 /* if the # of observations is less than 3, then corr cannot be calculated. */ quietly by cn : egen cm=mean(c) quietly by cn year: egen cdm=count(cd) quietly by cn : egen cdmm=mean(cdm) drop if cm==0 drop if cdmm==0 sort date cn by date: gen id=[_n] sort cn date gen corr_c_`2'=. drop c cm cd cdm cdmm keep cn id date `1' year `2' corr_c_`2' save mm_, replace egen idmax=max(id) local m=idmax local j=1 while `j'<=`m' { use mm_, clear keep if id==`j' sort year quietly by year: egen c=count(`1') quietly gen cd=0 quietly replace cd=1 if c~=0 quietly replace cd=. if c<=2 /* if the # of observations is less than 3, then corr cannot be calculated. */ quietly by year: egen cdm=count(cd) quietly gen year_cd=year*cd quietly egen minobs=min(year_cd) quietly egen maxobs=max(year_cd) local k=maxobs-minobs+1 keep if year>=minobs&year<=maxobs drop if c==0 drop if cdm==0 drop if maxobs==minobs sort year quietly by year: egen c2=count(`2') quietly gen cd2=0 quietly replace cd2=1 if c2~=0 quietly replace cd2=. if c2<=2 /* if the # of observations is less than 3, then corr cannot be calculated. */ quietly by year: egen cdm2=count(cd2) quietly gen year_cd2=year*cd2 quietly egen minobs2=min(year_cd2) quietly egen maxobs2=max(year_cd2) local k=maxobs2-minobs2+1 keep if year>=minobs2&year<=maxobs2 drop if c2==0 drop if cdm2==0 drop if maxobs2==minobs2 egen idy=group(year) egen idymax=max(idy) local k=idymax local n `k' forvalues i = 1(1)`n' { local n=`n'+1 correlate `1' `2' if idy==`i' replace corr_c_`2'=_result(4) if idy==`i' } drop c year_cd cd minobs maxobs cdm drop c2 year_cd2 cd2 minobs2 maxobs2 cdm2 replace corr_c_`2'=0 if corr_c_`2'==. save `2'`j', replace local j=`j'+1 } list id cn if date==480 end