typos in output

typos in output

	*! version 3.0 26DEC2016  Kristoffer Bjarkefur kbjarkefur@worldbank.org

	capture program drop iecompdup
	program iecompdup , rclass

	qui {

		syntax varname ,  id(string) [DIDIfference KEEPDIFFerence KEEPOTHer(varlist) more2ok]

		version 11.0

		preserve

			/****************************

				Turn ID var to string so that the rest of the command works similarly

			****************************/

			* Test if ID variable is numeric or string
			cap confirm numeric variable `varlist'
			if !_rc {

				* If numeric, test if all values in ID variable is integer
				cap assert mod(`varlist',1) == 0

				* This command does not allow numeric ID varaibels that are not integers
				if _rc {
					di as error "{phang}The ID variable is only allowed to be either string or only consist of integers. Integer in this context is not the same as the variable type int. Integer in this context means numeric values without decimals. Please consider using integers as your ID or convert your ID variable to a string.{p_end}"

				}
				else {

					** Find the longest (for integers that is the same as largest)
					*  number and get its legth.l
					sum `varlist'
					local length 	= strlen("`r(max)'")

					** Use that length when explicitly setting the format in
					*  order to prevent information lost
					tostring `varlist', replace format(%`length'.0f)
				}
			}

			* Testing that the ID variable is a string before constinueing the command
			cap confirm string variable `varlist'
			if _rc {
				di as error "{phang}This error message is not due to incorrect specification from you. This message follows a failed check that the command is working properly. If you get this error, please send an email to kbjarkefur@worldbank.org including the follwoing message 'ID var was not succesfully turned in to a string without information loss in iecompdup.' and include whatever other data you do not mind sharing.{p_end}"
			}

			* Only keep duplicates with the ID specified
			keep if  `varlist' == "`id'"

			/****************************

				Test if input is correct

			****************************/

			if "`keepdifference'" == "" & "`keepother'" != "" {

				noi di as error "{phang}Not allowed to specify keepother() without specifying keepdifference{p_end}"
				noi di ""
				error 197
				exit
			}

			* Test the number of observations left and make sure that there are only two of them
			count
			if `r(N)' == 0 {

				noi di as error "{phang}ID incorrectly specified. No observations with (`varlist' == `id'){p_end}"
				noi di ""
				error 2000
				exit
			}
			else if `r(N)' == 1 {

				noi di as error "{phang}ID incorrectly specified. No duplicates with that ID. Only one observation where (`varlist' == `id'){p_end}"
				noi di ""
				error 2001
				exit
			}
			else if `r(N)' > 2 & "`more2ok'" == "" {

				noi di as error "{phang}The current version of ie_compdup is not able to compare more than 2 duplicates at the time. (How to output the results for groups larger than 2 is non-obvious and suggestions on how to do that are appreciated.) Either drop one of the duplicates before re-running the command or specify option more2ok and the comparison will be done between the first and the second row.{p_end}"
				noi di ""
				error 198
				exit
			}
			else {

			/****************************

				Compare all variables

			****************************/

				* If more than 2 observations, keep the first and second row only
				keep if _n <= 2

				*Initiate the locals
				local match
				local difference

				* Go over all variables and see if they are non missing for at least one of the variables
				foreach var of varlist _all {

					cap assert missing(`var')

					** If not missing for at lease one of the observations, test
					*  if they are identical across the duplicates or not, and
					*  store variable name in appropriate local
					if _rc {

						* Are the variables identical
						if `var'[1] == `var'[2] {
							local match `match' `var'
						}
						else {
							local difference `difference' `var'
						}
					}
					* If missing for all duplicates, then drop that variable
					else {
						drop `var'
					}
				}

				* Remove the ID var from the match list, it is match by definition and therefore add no information
				local match : list match - varlist

			/****************************

				Output the result

			****************************/

				noi di ""
				** Display all variables that differ. This comes first in case
				*  the number of variables are a lot, cause then it would push
				*  any other output to far up
				if "`didifference'" != "" {

					noi di "{phang}The following variables have different values across the duplicates:{p_end}"
					noi di "{pstd}`difference'{p_end}"
					noi di ""
				}

				*Display number output
				local numNonMissing = `:list sizeof match' + `:list sizeof difference'

				noi di "{phang}The duplicate observations with ID = `id' have non-missing values in `numNonMissing' variables. Out of those variables:{p_end}"
				noi di ""
				noi di "{phang2}`:list sizeof match' variable(s) are identical across the duplicates{p_end}"
				noi di "{phang2}`:list sizeof difference' variable(s) have different values across the duplicates{p_end}"

				return local matchvars 	`"`match'"'
				return local diffvars 	`"`difference'"'

				return scalar nummatch	= `:list sizeof match'
				return scalar numdiff 	= `:list sizeof difference'
				return scalar numnomiss	= `numNonMissing'
		}

		restore

		* If keep difference is applied only keep those variables here.
		if "`keepdifference'" != "" & "`difference'" != "" {

			order `varlist' `difference' `keepother'
			keep `varlist' `difference' `keepother'

			* Drop differently depending on numeric or string
			cap confirm numeric variable `varlist'
			if !_rc {
				keep if  `varlist' == `id'
			}
			else {
				keep if  `varlist' == "`id'"
			}
		}
	}
	end

	*! version 3.0 26DEC2016  Kristoffer Bjarkefur kbjarkefur@worldbank.org

	capture program drop iecompdup
	program iecompdup , rclass

	qui {

		syntax varname ,  id(string) [DIDIfference KEEPDIFFerence KEEPOTHer(varlist) more2ok]

		version 11.0

		preserve

			/****************************

				Turn ID var to string so that the rest of the command works similarly

			****************************/

			* Test if ID variable is numeric or string
			cap confirm numeric variable `varlist'
			if !_rc {

				* If numeric, test if all values in ID variable is integer
				cap assert mod(`varlist',1) == 0

				* This command does not allow numeric ID varaibels that are not integers
				if _rc {
					di as error "{phang}The ID variable is only allowed to be either string or only consist of integers. Integer in this context is not the same as the variable type int. Integer in this context means numeric values without decimals. Please consider using integers as your ID or convert your ID variable to a string.{p_end}"

				}
				else {

					** Find the longest (for integers that is the same as largest)
					*  number and get its legth.l
					sum `varlist'
					local length 	= strlen("`r(max)'")

					** Use that length when explicitly setting the format in
					*  order to prevent information lost
					tostring `varlist', replace format(%`length'.0f)
				}
			}

			* Testing that the ID variable is a string before constinueing the command
			cap confirm string variable `varlist'
			if _rc {
				di as error "{phang}This error message is not due to incorrect specification from you. This message follows a failed check that the command is working properly. If you get this error, please send an email to kbjarkefur@worldbank.org including the follwoing message 'ID var was not succesfully turned in to a string without information loss in iecompdup.' and include whatever other data you do not mind sharing.{p_end}"
			}

			* Only keep duplicates with the ID specified
			keep if  `varlist' == "`id'"

			/****************************

				Test if input is correct

			****************************/

			if "`keepdifference'" == "" & "`keepother'" != "" {

				noi di as error "{phang}Not allowed to specify keepother() without specifying keepdifference{p_end}"
				noi di ""
				error 197
				exit
			}

			* Test the number of observations left and make sure that there are only two of them
			count
			if `r(N)' == 0 {

				noi di as error "{phang}ID incorrectly specified. No observations with (`varlist' == `id'){p_end}"
				noi di ""
				error 2000
				exit
			}
			else if `r(N)' == 1 {

				noi di as error "{phang}ID incorrectly specified. No duplicates with that ID. Only one observation where (`varlist' == `id'){p_end}"
				noi di ""
				error 2001
				exit
			}
			else if `r(N)' > 2 & "`more2ok'" == "" {

				noi di as error "{phang}The current version of ie_compdup is not able to compare more than 2 duplicates at the time. (How to output the results for groups larger than 2 is non-obvious and suggestions on how to do that are appreciated.) Either drop one of the duplicates before re-running the command or specify option more2ok and the comparison will be done between the first and the second row.{p_end}"
				noi di ""
				error 198
				exit
			}
			else {

			/****************************

				Compare all variables

			****************************/

				* If more than 2 observations, keep the first and second row only
				keep if _n <= 2

				*Initiate the locals
				local match
				local difference

				* Go over all variables and see if they are non missing for at least one of the variables
				foreach var of varlist _all {

					cap assert missing(`var')

					** If not missing for at lease one of the observations, test
					*  if they are identical across the duplicates or not, and
					*  store variable name in appropriate local
					if _rc {

						* Are the variables identical
						if `var'[1] == `var'[2] {
							local match `match' `var'
						}
						else {
							local difference `difference' `var'
						}
					}
					* If missing for all duplicates, then drop that variable
					else {
						drop `var'
					}
				}

				* Remove the ID var from the match list, it is match by definition and therefore add no information
				local match : list match - varlist

			/****************************

				Output the result

			****************************/

				noi di ""
				** Display all variables that differ. This comes first in case
				*  the number of variables are a lot, cause then it would push
				*  any other output to far up
				if "`didifference'" != "" {

					noi di "{phang}The following variables have different values across the duplicates:{p_end}"
					noi di "{pstd}`difference'{p_end}"
					noi di ""
				}

				*Display number output
				local numNonMissing = `:list sizeof match' + `:list sizeof difference'

				noi di "{phang}The duplicate observations with ID = `id' have non-missing values in `numNonMissing' variables. Out of those variables:{p_end}"
				noi di ""
				noi di "{phang2}`:list sizeof match' varaible(s) are identical across the duplicates{p_end}"
				noi di "{phang2}`:list sizeof difference' varaible(s) have different values across the duplicates{p_end}"

				return local matchvars 	`"`match'"'
				return local diffvars 	`"`difference'"'

				return scalar nummatch	= `:list sizeof match'
				return scalar numdiff 	= `:list sizeof difference'
				return scalar numnomiss	= `numNonMissing'
		}

		restore

		* If keep difference is applied only keep those variables here.
		if "`keepdifference'" != "" & "`difference'" != "" {

			order `varlist' `difference' `keepother'
			keep `varlist' `difference' `keepother'

			* Drop differently depending on numeric or string
			cap confirm numeric variable `varlist'
			if !_rc {
				keep if  `varlist' == `id'
			}
			else {
				keep if  `varlist' == "`id'"
			}
		}
	}
	end


finalize

finalize

program ddpic

local var `1'
local isbaseball `2'

run_reg `var' `isbaseball'

make_pic `var' `isbaseball'

end

program run_reg

use ${stash}master, clear

local var `1'
local isbaseball `2'

drop if isbaseball != `isbaseball'

qui xtreg `var' 1.treat##b2008.year, fe vce(robust)

qui parmest, label list(parm estimate min* max* p) saving(${stash}mypars, replace)

end

program make_pic

local var `1'
local isbaseball `2'

clear
use ${stash}mypars, clear

keep if regexm(parm, "1.*treat#20[0-9][0-9].*") == 1
gen year = regexs(1) if regexm(parm, ".*#(20[0-9][0-9])b?\..*")
destring year, replace

drop if year < 2005

qui gen xaxis = 0

if "`isbaseball'" == "0"{
   local vartitle = "Basketball"
   local yt = ""
 }

if "`isbaseball'" == "1"{
   local vartitle = "Baseball"
   local yt = "Mean value / page"
 }

if "`var'" == "img" {
    local scale "yscale(range(-0.3 0.8)) ylabel(-0.2 (0.2) 0.8)"
}

if "`var'" == "text" {
    local scale "yscale(range(-3500 3000)) ylabel(-3000 (1000) 3000)"
}

if "`var'" == "traf" {
    drop if year == 2013
    local scale "yscale(range(-100 50)) ylabel(-100 (25) 50)"
}

graph twoway (connected estimate year, msize(small) lpattern(dash) lcolor(edkblue) lwidth(thin)) (line max year, lwidth(vthin) lpattern(-) lcolor(gs8)) (line min year, lwidth(vthin) lpattern(-) lcolor(gs8)) (line xaxis year, lwidth(vthin) lcolor(gs8)), xtitle("") ytitle("`yt'") xlabel(2005(1)2013) legend(off) title("`vartitle' Players") `scale'

** ylabel(-0.2 (0.2) 0.8)

graph export "${tables}timeline_`var'_`isbaseball'.eps", replace
shell epstopdf  "${tables}timeline_`var'_`isbaseball'.eps"

end

program ddpic

local var `1'
local isbaseball `2'

run_reg `var' `isbaseball'

make_pic `var' `isbaseball'

end

program run_reg

use ${stash}master, clear

local var `1'
local isbaseball `2'

drop if isbaseball != `isbaseball'

qui xtreg `var' 1.treat##b2008.year, fe vce(robust)

qui parmest, label list(parm estimate min* max* p) saving(${stash}mypars, replace)

end

program make_pic

local var `1'
local isbaseball `2'

clear
use ${stash}mypars, clear

keep if regexm(parm, "1.*treat#20[0-9][0-9].*") == 1
gen year = regexs(1) if regexm(parm, ".*#(20[0-9][0-9])b?\..*")
destring year, replace

drop if year < 2005

qui gen xaxis = 0

if "`isbaseball'" == "0"{
   local vartitle = "Basketball"
   local yt = ""
 }

if "`isbaseball'" == "1"{
   local vartitle = "Baseball"
   local yt = "Mean value / page"
 }

if "`var'" == "img" {
    local scale "yscale(range(-0.3 0.8)) ylabel(-0.2 (0.2) 0.8)"
}

if "`var'" == "text" {
    local scale "yscale(range(0 3000)) ylabel(0 (500) 3000)"
}

if "`var'" == "traf" {
    drop if year == 2013
    local scale "yscale(range(-100 50)) ylabel(-100 (25) 50)"
}

graph twoway (connected estimate year, msize(small) lpattern(dash) lcolor(edkblue) lwidth(thin)) (line max year, lwidth(vthin) lpattern(-) lcolor(gs8)) (line min year, lwidth(vthin) lpattern(-) lcolor(gs8)) (line xaxis year, lwidth(vthin) lcolor(gs8)), xtitle("") ytitle("`yt'") xlabel(2005(1)2013) legend(off) title("`vartitle' Players") `scale'

** ylabel(-0.2 (0.2) 0.8)

graph export "${tables}timeline_`var'_`isbaseball'.eps", replace
shell epstopdf  "${tables}timeline_`var'_`isbaseball'.eps"

end