iefodler : help explicit {p_end}

iefodler : help explicit {p_end}

{smcl}
{* 25 May 2017}{...}
{hline}
help for {hi:iefolder}
{hline}

{title:Title}

{phang2}{cmdab:iefolder} {hline 2} sets up project folders and master do-files according to World Bank DIME's standards.{p_end}

{title:Syntax}

{pstd} {ul:When initially setting up the {hi:DataWork} folder in a new project:}{p_end}

{phang2}{cmd:iefolder} new project, {cmdab:proj:ectfolder(}{it:directory}{cmd:)} {p_end}

{pstd} {ul:When adding folders to and already existing {hi:DataWork} folder:}{p_end}

{phang2}{cmd:iefolder} new {it:itemtype} {it:itemname} , {cmdab:proj:ectfolder(}{it:directory}{cmd:)}
	[{cmdab:abb:reviation(}{it:string}{cmd:)}] {p_end}

{pmore}where {it:itemtype} is either {it:round} or {it:unitofobs}. See
	details on {it:itemtype} and {it:itemname} below.{p_end}

{marker opts}{...}
{synoptset 22}{...}
{synopthdr:options}
{synoptline}
{synopt : {cmdab:proj:ectfolder(}{it:dir}{cmd:)}}The location of
	the project folder where the {hi:DataWork} folder should be
	created (new projects), or where it is located (new round and new unitofobs).{p_end}
{synopt : {cmdab:abb:reviation(}{it:string}{cmd:)}}Optional abbreviation
	of round name to be used to make globals shorter. Can only be used
	when {it:itemtype} is round.{p_end}
{synoptline}

{marker desc}{title:Description}

{pstd}{cmdab:iefolder} automates the process of setting up the folders and master do-files
	where all the data work will take place in a project folder. The folders set
	up will follow DIME's best practices outlined and explained here:
	{browse "https://dimewiki.worldbank.org/wiki/DataWork_Folder"} (This page
	is a part of a Wiki that we are in the final stage of getting approval to
	release externally, the page is until then unfortunately password protected.)

{pstd}In addition to setting up the {hi:DataWork} folder and its sub-folders, the
	command creates master do-files linking to all of these sub-folders. These
	master do-files are updated whenever more subfolders are added using this command.

{pstd}{ul:{hi:itemtypes}}. This command can create either a new DataWork folder or add folders to an
	existing DataWork folder. The existing DataWork folder must have been created
	with {cmd:iefolder} for the additions to work. There are two types of folders
	that can be added to an existing folder, {ul:round} and {ul:untiofobs}. See
	next paragraphs for descriptions.

{pstd}{hi:{it:round}} folders are folders specific to a data collection round, for example, {it:Baseline}, {it:Endline},
	{it:Follow Up} etc. When adding a new round, sub-folders are added to the DataWork
	folder in line with the best practice described here:
	{browse "https://dimewiki.worldbank.org/wiki/DataWork_Survey_Round"}. {cmd:iefolder} also
	creates a master do-file specific for this round with globals referencing the sub-folders
	specific to this round. {cmd:iefolder} is implemented so that you can keep working
	for years with your project in between adding folders. The command reads and perserves
	changes made manually to the DataWork folder and master do-file before making additions
	when adding a new round.

{pstd}{hi:{it:untiofobs}} folders are folders specific to a unit of observation,
	for example the master data set folder. Read more about master data sets and the folder structure
	this commands sets up for you at {browse "https://dimewiki.worldbank.org/wiki/Master_Data_Set"}. A
	master data folder for each new unit of observation is created in two places. Both in
	the MasterData folder in the DataWork folder, and in the MasterKeyID folder in the encrypted folder.

{marker optslong}{title:Options}

{phang}{cmdab:proj:ectfolder(}{it:dir}{cmd:)} should point to the same folder regardless
	of which {it:itemtype} is created. If {it:new project} is specified the file path should
	point to where DataWork should be created, and if {it:new round} or {it:new project} is
	specified, it should point to where DataWork was already created. See how the file path is
	the same both time when {cmd:iefolder} is called twice in Example 1 below.

{phang}{cmdab:abb:reviation(}{it:string}{cmd:)} can be used to shorten the globals created
	in the master do-files that point to the sub-folders to {it:round} folders. For example,
	if you create a new {it:round} called Baseline, as in Example 1, then a global to the
	DataSet folder called Baseline_dt will be created in the master do-file. If the
	abbreviation option would have been used, like in Example 2, then the global would
	have been called BL_dt.

{title:Examples}

{pstd}{hi:Example 1.}

{pmore}{inp:global projectFolder "C:\Users\Documents\DropBox\ProjectABC"}

{pmore}{inp:iefolder new project , projectfolder("$projectFolder")}{break}
{inp:iefolder new round baseline , projectfolder("$projectFolder")}

{pstd}In the example above, in the line the first time {cmd:iefolder} is used, a folder
	called {hi:DataWork} is created at the location of {it:"C:\Users\Documents\DropBox\ProjectABC"}.
	In the line where {cmd:iefolder} used a second time, a folder for the baseline round
	is created inside the {hi:DataWork} folder. Note that the folder provided
	in {inp:projectfolder()} is the same both times.

{pstd}Both the {hi:DataWork} folder and the {hi:baseline} folder created have sub-folders
	and master do-files creating globals pointing to these.

{pstd}{hi:Example 2.}

{pmore}{inp:global projectFolder "C:\Users\Documents\DropBox\ProjectABC"}

{pmore}{inp:iefolder new project , projectfolder("$projectFolder")}{break}
{inp:iefolder new round baseline , projectfolder("$projectFolder") abbreviation("BL")}

{pstd}The example above creates the same folder structure as in Example 1, but
	in the globals in the master do-files the abbreviation BL is used instead of
	baseline. But the folders created on disk will still be called baseline.

{pstd} {hi:Example 3.}

{pstd}The example below is meant to describe an intended workflow for a full
	life cycle of an impact evaluation. First we need to set up the {hi:DataWork} folder.
	We do that using {it:{cmd:iefolder} new project}. Like this:

{pmore}{inp:iefolder new project , projectfolder("C:\Users\Documents\DropBox\ProjectABC")}

{pstd}Our first data collection will be a baseline where the unit of observation
	is households. We therefore need to set up folders for the unit of observation
	"households". In the encrypted master folder for "housholds" you can create your
	list over households and you have a subfolder called {it:Sampling} where you
	can keep do files and data with identifying infomration. We create all of that by
	using {it:{cmd:iefolder} new untiofobs household}.	Like this:

{pmore}{inp:iefolder new untiofobs household , projectfolder("C:\Users\Documents\DropBox\ProjectABC") abbreviation("BL")}

{pstd}When we are ready to start preparing for the baseline we want to create the
	baseline folder. We do that using {it:{cmd:iefolder} new round baseline}. Like this:

{pmore}{inp:iefolder new round baseline , projectfolder("C:\Users\Documents\DropBox\ProjectABC") abbreviation("BL")}

{pstd}At this point we can collect the baseline data, save the data in the folders
	we created and write the report. Then long stretches of time might pass before
	we need to use the command.

{pstd}Let's say that when we plan for midline we also want
	to collect data about the villages that the households we interview in baseline
	live in. Then we need to create a new master folder for the unit of observation
	villages. We do that using {it:{cmd:iefolder} new untiofobs village}. Like this:

{pmore}{inp:iefolder new untiofobs village , projectfolder("$projectFolder")}

{pstd}Then we need to create the rounds used for the midline round for both
	households and for villages. Since this is separate data collection (although
	they might happen at the same time). We create those folders like this:

{pmore}{inp:iefolder new round midlineVillage , projectfolder("$projectFolder") abbreviation("ML")}{break}
{inp:iefolder new round midline {space 6}     , projectfolder("$projectFolder") abbreviation("MLvill")}

{pstd}Finally, in the last round of data collection, we are only collecting data
	on households again. Since we are not collecting data on any new unit
	of observation, we do not need to create any new master folder.

{pmore}{inp:iefolder new round endline , projectfolder("$projectFolder") abbreviation("EL")}

{title:Acknowledgements}

{phang}I would like to acknowledge the help in testing and proofreading I received in relation to this command and help file from (in alphabetic order):{p_end}
{pmore}Luiza Cardoso De Andrade{break}Laura Costica{break}Seungmin Lee{break}Mrijan Rimal{break}Sakina Shibuya

{title:Author}

{phang}Kristoffer Bjarkefur, The World Bank, DECIE

{phang}Please send bug-reports, suggestions and requests for clarifications
		 writing "ietoolkit iefolder" in the subject line to:{break}
		 kbjarkefur@worldbank.org

{phang}You can also see the code, make comments to the code, see the version
		 history of the code, and submit additions or edits to the code through
		 the github repository of ietoolkit:{break}
		 {browse "https://github.com/worldbank/ietoolkit"}

{smcl}
{* 25 May 2017}{...}
{hline}
help for {hi:iefolder}
{hline}

{title:Title}

{phang2}{cmdab:iefolder} {hline 2} sets up project folders and master do-files according to World Bank DIME's standards.

{title:Syntax}

{pstd} {ul:When initially setting up the {hi:DataWork} folder in a new project:}

{phang2}{cmd:iefolder} new project, {cmdab:proj:ectfolder(}{it:directory}{cmd:)}

{pstd} {ul:When adding folders to and already existing {hi:DataWork} folder:}

{phang2}{cmd:iefolder} new {it:itemtype} {it:itemname} , {cmdab:proj:ectfolder(}{it:directory}{cmd:)}
	[{cmdab:abb:reviation(}{it:string}{cmd:)}]

{pmore}where {it:itemtype} is either {it:round} or {it:unitofobs}. See
	details on {it:itemtype} and {it:itemname} below.

{marker opts}{...}
{synoptset 22}{...}
{synopthdr:options}
{synoptline}
{synopt : {cmdab:proj:ectfolder(}{it:dir}{cmd:)}}The location of
	the project folder where the {hi:DataWork} folder should be
	created (new projects), or where it is located (new round and new unitofobs).{p_end}
{synopt : {cmdab:abb:reviation(}{it:string}{cmd:)}}Optional abbreviation
	of round name to be used to make globals shorter. Can only be used
	when {it:itemtype} is round.{p_end}
{synoptline}

{marker desc}{title:Description}

{pstd}{cmdab:iefolder} automates the process of setting up the folders and master do-files
	where all the data work will take place in a project folder. The folders set
	up will follow DIME's best practices outlined and explained here:
	{browse "https://dimewiki.worldbank.org/wiki/DataWork_Folder"} (This page
	is a part of a Wiki that we are in the final stage of getting approval to
	release externally, the page is until then unfortunately password protected.)

{pstd}In addition to setting up the {hi:DataWork} folder and its sub-folders, the
	command creates master do-files linking to all of these sub-folders. These
	master do-files are updated whenever more subfolders are added using this command.

{pstd}{ul:{hi:itemtypes}}. This command can create either a new DataWork folder or add folders to an
	existing DataWork folder. The existing DataWork folder must have been created
	with {cmd:iefolder} for the additions to work. There are two types of folders
	that can be added to an existing folder, {ul:round} and {ul:untiofobs}. See
	next paragraphs for descriptions.

{pstd}{hi:{it:round}} folders are folders specific to a data collection round, for example, {it:Baseline}, {it:Endline},
	{it:Follow Up} etc. When adding a new round, sub-folders are added to the DataWork
	folder in line with the best practice described here:
	{browse "https://dimewiki.worldbank.org/wiki/DataWork_Survey_Round"}. {cmd:iefolder} also
	creates a master do-file specific for this round with globals referencing the sub-folders
	specific to this round. {cmd:iefolder} is implemented so that you can keep working
	for years with your project in between adding folders. The command reads and perserves
	changes made manually to the DataWork folder and master do-file before making additions
	when adding a new round.

{pstd}{hi:{it:untiofobs}} folders are folders specific to a unit of observation,
	for example the master data set folder. Read more about master data sets and the folder structure
	this commands sets up for you at {browse "https://dimewiki.worldbank.org/wiki/Master_Data_Set"}. A
	master data folder for each new unit of observation is created in two places. Both in
	the MasterData folder in the DataWork folder, and in the MasterKeyID folder in the encrypted folder.

{marker optslong}{title:Options}

{phang}{cmdab:proj:ectfolder(}{it:dir}{cmd:)} should point to the same folder regardless
	of which {it:itemtype} is created. If {it:new project} is specified the file path should
	point to where DataWork should be created, and if {it:new round} or {it:new project} is
	specified, it should point to where DataWork was already created. See how the file path is
	the same both time when {cmd:iefolder} is called twice in Example 1 below.

{phang}{cmdab:abb:reviation(}{it:string}{cmd:)} can be used to shorten the globals created
	in the master do-files that point to the sub-folders to {it:round} folders. For example,
	if you create a new {it:round} called Baseline, as in Example 1, then a global to the
	DataSet folder called Baseline_dt will be created in the master do-file. If the
	abbreviation option would have been used, like in Example 2, then the global would
	have been called BL_dt.

{title:Examples}

{pstd}{hi:Example 1.}

{pmore}{inp:global projectFolder "C:\Users\Documents\DropBox\ProjectABC"}

{pmore}{inp:iefolder new project , projectfolder("$projectFolder")}{break}
{inp:iefolder new round baseline , projectfolder("$projectFolder")}

{pstd}In the example above, in the line the first time {cmd:iefolder} is used, a folder
	called {hi:DataWork} is created at the location of {it:"C:\Users\Documents\DropBox\ProjectABC"}.
	In the line where {cmd:iefolder} used a second time, a folder for the baseline round
	is created inside the {hi:DataWork} folder. Note that the folder provided
	in {inp:projectfolder()} is the same both times.

{pstd}Both the {hi:DataWork} folder and the {hi:baseline} folder created have sub-folders
	and master do-files creating globals pointing to these.

{pstd}{hi:Example 2.}

{pmore}{inp:global projectFolder "C:\Users\Documents\DropBox\ProjectABC"}

{pmore}{inp:iefolder new project , projectfolder("$projectFolder")}{break}
{inp:iefolder new round baseline , projectfolder("$projectFolder") abbreviation("BL")}

{pstd}The example above creates the same folder structure as in Example 1, but
	in the globals in the master do-files the abbreviation BL is used instead of
	baseline. But the folders created on disk will still be called baseline.

{pstd} {hi:Example 3.}

{pstd}The example below is meant to describe an intended workflow for a full
	life cycle of an impact evaluation. First we need to set up the {hi:DataWork} folder.
	We do that using {it:{cmd:iefolder} new project}. Like this:

{pmore}{inp:iefolder new project , projectfolder("C:\Users\Documents\DropBox\ProjectABC")}

{pstd}Our first data collection will be a baseline where the unit of observation
	is households. We therefore need to set up folders for the unit of observation
	"households". In the encrypted master folder for "housholds" you can create your
	list over households and you have a subfolder called {it:Sampling} where you
	can keep do files and data with identifying infomration. We create all of that by
	using {it:{cmd:iefolder} new untiofobs household}.	Like this:

{pmore}{inp:iefolder new untiofobs household , projectfolder("C:\Users\Documents\DropBox\ProjectABC") abbreviation("BL")}

{pstd}When we are ready to start preparing for the baseline we want to create the
	baseline folder. We do that using {it:{cmd:iefolder} new round baseline}. Like this:

{pmore}{inp:iefolder new round baseline , projectfolder("C:\Users\Documents\DropBox\ProjectABC") abbreviation("BL")}

{pstd}At this point we can collect the baseline data, save the data in the folders
	we created and write the report. Then long stretches of time might pass before
	we need to use the command.

{pstd}Let's say that when we plan for midline we also want
	to collect data about the villages that the households we interview in baseline
	live in. Then we need to create a new master folder for the unit of observation
	villages. We do that using {it:{cmd:iefolder} new untiofobs village}. Like this:

{pmore}{inp:iefolder new untiofobs village , projectfolder("$projectFolder")}

{pstd}Then we need to create the rounds used for the midline round for both
	households and for villages. Since this is separate data collection (although
	they might happen at the same time). We create those folders like this:

{pmore}{inp:iefolder new round midlineVillage , projectfolder("$projectFolder") abbreviation("ML")}{break}
{inp:iefolder new round midline {space 6}     , projectfolder("$projectFolder") abbreviation("MLvill")}

{pstd}Finally, in the last round of data collection, we are only collecting data
	on households again. Since we are not collecting data on any new unit
	of observation, we do not need to create any new master folder.

{pmore}{inp:iefolder new round endline , projectfolder("$projectFolder") abbreviation("EL")}

{title:Acknowledgements}

{phang}I would like to acknowledge the help in testing and proofreading I received in relation to this command and help file from (in alphabetic order):{p_end}
{pmore}Luiza Cardoso De Andrade{break}Laura Costica{break}Seungmin Lee{break}Mrijan Rimal{break}Sakina Shibuya

{title:Author}

{phang}Kristoffer Bjarkefur, The World Bank, DECIE

{phang}Please send bug-reports, suggestions and requests for clarifications
		 writing "ietoolkit iefolder" in the subject line to:{break}
		 kbjarkefur@worldbank.org

{phang}You can also see the code, make comments to the code, see the version
		 history of the code, and submit additions or edits to the code through
		 the github repository of ietoolkit:{break}
		 {browse "https://github.com/worldbank/ietoolkit"}


fixed mkdir

fixed mkdir

//*! Apoorva Lal V-0.1
// creates an entire path at once (rather than 1 folder at a time with mkdir)
pr def pathmake
	version 10
	args p
	loc cwd = "`c(pwd)'"
	loc makedir mkdir
	if "`p'" == "" {
		di as error "pathmake needs an argument"
		error
	}
	loc p = subinstr("`p'","\","/",.)
	tokenize "`p'", p("/")
	loc st = 1
	loc tmp = ""
	if substr("`p'",1,2) == "//" {
		forv i = 1/5 {
			loc temp "`temp'``i''"
		}
		qui cd "`temp'"
		loc st = 6
	}
	loc i = `st'
	while "``i''" != "" {
		if "``i''" != "/" & "`"i"'" != "" {
			cap cd "``i''/"
			if _rc != 0 {
				`makedir' "``i''"
				qui cd "``i''/"
				if "``=`i'+1''" == "" {
					di in white "pathmake: generated path ", _c
					pwd
				}
			}
			if _rc == 0 & "``=`i'+1''" == "" {
				di in white "pathmake: path already exists;", _c
				pwd
			}
		}
		loc ++i
	}
	qui cd "`cwd'"
end

//*! Apoorva Lal V-0.1
// creates an entire path at once (rather than 1 folder at a time with mkdir)
pr def pathmake
	version 10
	args p
	loc cwd = "`c(pwd)'"
	if "`c(os)'" == "Windows" {
		loc makedir mkdir
	}
	else {
		loc makedir md
	}
	if "`p'" == "" {
		di as error "pathmake needs an argument"
		error
	}
	loc p = subinstr("`p'","\","/",.)
	tokenize "`p'", p("/")
	loc st = 1
	loc tmp = ""
	if substr("`p'",1,2) == "//" {
		forv i = 1/5 {
			loc temp "`temp'``i''"
		}
		qui cd "`temp'"
		loc st = 6
	}
	loc i = `st'
	while "``i''" != "" {
		if "``i''" != "/" & "`"i"'" != "" {
			cap cd "``i''/"
			if _rc != 0 {
				`makedir' "``i''"
				qui cd "``i''/"
				if "``=`i'+1''" == "" {
					di in white "pathmake: generated path ", _c
					pwd
				}
			}
			if _rc == 0 & "``=`i'+1''" == "" {
				di in white "pathmake: path already exists;", _c
				pwd
			}
		}
		loc ++i
	}
	qui cd "`cwd'"
end


Removed most of the noi di used during testing

cap	program drop	ieimpgraph
	program define 	ieimpgraph
	preserve
	syntax varlist, [noconfbars TItle(string) save(string) ]

	mat beta_ = e(b)

	local counter = 0
	//local textfile = "Mrijan"

	qui{

		if "`confbars'" 			!= "" {
		local CONFINT_BAR 	= 0
		}
		else if "`confbars'" 			== "" {
		local CONFINT_BAR 	= 1
		}

		foreach var of local varlist{
			cap assert inlist(`var',0,1) | missing(`var')
				if _rc {
					noi display as error "{phang} The variable `var' is not a dummy. Treatment variable needs to be a dummy(0 or 1) variable. {p_end}"
					noi display ""
					error 149
					}
				else {
					}

		local counter = `counter' + 1
		di `counter'

		scalar coeff_`var' = _b[`var']
		//noi display coeff_`var'

		scalar coeff_se_`var' = _se[`var']
		//noi display coeff_se_`var'

		count if e(sample) == 1 & `var' == 1 //Count one tmt group at the time
		scalar obs_`var' = r(N)
		//noi display obs_`var'

	}

	local count: word count `varlist'
	local graphCount = `count' + 1
	//tokenize "`varlist'"
	//noi di `count'

	//Make all vars tempvars (maybe do later)
	//Make sure that missing is properly handled
	tempvar anyTMT
	egen `anyTMT' = rowmax(`varlist') if e(sample) == 1
	tempvar control
	gen `control' = (`anyTMT' == 0) if !missing(`anyTMT')

	sum `e(depvar)' if e(sample) == 1 & `control' == 1
	scalar ctl_N		= r(N)
	scalar ctl_mean	  	= r(mean)
	scalar ctl_mean_sd 	= r(sd)

		foreach var of local varlist {
			scalar  conf_int_min_`var'   =	coeff_`var'-(1.96*coeff_se_`var') + ctl_mean
			scalar  conf_int_max_`var'   =	coeff_`var'+(1.96*coeff_se_`var') + ctl_mean

			test `var'
			local star_`var' " "

			scalar  pvalue =r(p)
			if pvalue < 0.10 {
				local star_`var' "*"
			}
			if pvalue < 0.05 {
				local star_`var' "**"
			}
			if pvalue < 0.01 {
				local star_`var' "***"
			}
			*noi display "`star_`var''"
			scalar tmt_mean_`var' = ctl_mean + coeff_`var'
		}

	tempname 	newHandle
	tempfile	newTextFile
	cap file close 	`newHandle'
	file open  	`newHandle' using "`newTextFile'", text write append

	*Write headers and control value
	file write `newHandle' ///
		"order" 	_tab "xLabeled"	_tab "mean" _tab "coeff" _tab "conf_int_min" _tab "conf_int_max" _tab "obs" _tab "star" _n 	///
		%9.3f (1) _tab "Control"  _tab %9.3f (ctl_mean)      _tab  		 			_tab 		 			  	  _tab 			 				_tab %9.3f (ctl_N) 	   _tab			  	  _n

	tempvar newCounter
	gen `newCounter' = 2  //First tmt group starts at 2 (1 is control)

	foreach var in `varlist' {

		file write `newHandle' ///
			%9.3f (`newCounter') _tab "`var'" _tab %9.3f (tmt_mean_`var') _tab  %9.3f (coeff_`var')  _tab %9.3f (conf_int_min_`var') _tab %9.3f (conf_int_max_`var') _tab %9.3f (obs_`var')   _tab "`star_`var''"  _n

		replace `newCounter' = `newCounter' + 1
	}

	file close `newHandle'

	copy "`newTextFile'"  "mainMasterDofile.txt" , replace

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

		Create the graph

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

	insheet using mainMasterDofile.txt, clear

	local tmtGroupBars 	""
	local xAxisLabels 	`"xlabel( "'
	local legendLabels	`"lab(1 "`obsLabelControl'")"'
	local legendNumbers	""

	forval tmtGroupCount = 1/`graphCount' {

		************
		*Create the bar for this group

		colorPicker `tmtGroupCount' `graphCount'

		local tmtGroupBars `"`tmtGroupBars' (bar mean order if order == `tmtGroupCount', color("`r(color)'")) "'

		************
		*Create labels etc. for this group

		local obs 			= obs[`tmtGroupCount']
		local stars 		= star[`tmtGroupCount']
		local legendLabel 	= xlabeled[`tmtGroupCount']

		local xAxisLabels 	`"`xAxisLabels' `tmtGroupCount' "(N = `obs') `stars'" "'
		local legendLabels 	`"`legendLabels' lab(`tmtGroupCount' "`legendLabel'") "'
		local legendNumbers	`"`legendNumbers' `tmtGroupCount'"'
	}

	*Close or comple some strings
	local xAxisLabels `"`xAxisLabels' ,noticks labsize(medsmall)) "'
	local legendOption `"legend(order(`legendNumbers') `legendLabels')"'

	*Create the confidence interval bars
	if `CONFINT_BAR' == 0 {
	//local confIntGraph == `"(scatter mean order,  msym(none)  mlabs(medium) mlabpos(10) mlabcolor(black)), xtitle("") ytitle("`e(depvar)'") "'
	local confIntGraph = `", xtitle("") ytitle("`e(depvar)'") "'
	}
	else if `CONFINT_BAR' == 1 {
	local confIntGraph = `"(rcap conf_int_max conf_int_min order, lc(gs)) (scatter mean order,  msym(none)  mlabs(medium) mlabpos(10) mlabcolor(black)), xtitle("") ytitle("`e(depvar)'")  "'
	}

	if "`save'" != "" {
		local saveOption saving("`save'", replace)
	}

	noi di `" graph twoway `tmtGroupBars' `confIntGraph' `legendOption' `xAxisLabels' `saveOption' title("`title'")   "'
	graph twoway `tmtGroupBars' `confIntGraph' `legendOption' `xAxisLabels' `saveOption' title("`title'")
	//noi di 	`"graph twoway `tmtGroupBars' `legendOption' (scatter mean order,  msym(none)  mlabs(medium) mlabpos(10) mlabcolor(black))), xtitle("") ytitle("`e(depvar)'") `xAxisLabels' `saveOption' title("`title'")  "'

	restore
}

end

cap	program drop	colorPicker
	program define 	colorPicker , rclass

	args groupCount totalNumGroups

	if `totalNumGroups' == 2 {

		if `groupCount' == 1 return local color "215 25 28"
		if `groupCount' == 2 return local color "43 123 182"
	}
	else if `totalNumGroups' == 3 {

		if `groupCount' == 1 return local color "215 25 28"
		if `groupCount' == 2 return local color "255 255 191"
		if `groupCount' == 3 return local color "43 123 182"
	}
	else if `totalNumGroups' == 4 {

		if `groupCount' == 1 return local color "215 25 28"
		if `groupCount' == 2 return local color "255 255 191"
		if `groupCount' == 3 return local color "171 217 233"
		if `groupCount' == 4 return local color "43 123 182"
	}
	else {

		*For five or more colors we repeat the same pattern

		local colourNum = mod(`groupCount', 5)
		if `colourNum' == 1 return local color "215 25 28"
		if `colourNum' == 2 return local color "253 174 93"
		if `colourNum' == 3 return local color "255 255 191"
		if `colourNum' == 4 return local color "171 217 233"
		if `colourNum' == 0 return local color "43 123 182"

	}

end

cap	program drop	ieimpgraph
	program define 	ieimpgraph
	preserve
	syntax varlist, [noconfbars TItle(string) save(string) ]

	mat beta_ = e(b)

	local counter = 0
	//local textfile = "Mrijan"
	//local date = "$S_DATE"

	qui{

		if "`confbars'" 			!= "" {
		local CONFINT_BAR 	= 0
		}
		else if "`confbars'" 			== "" {
		local CONFINT_BAR 	= 1
		}

		noi di "`CONFINT_BAR'"

		foreach var of local varlist{
			cap assert inlist(`var',0,1) | missing(`var')
				if _rc {
					noi display as error "{phang} The variable `var' is not a dummy. Treatment variable needs to be a dummy(0 or 1) variable. {p_end}"
					noi display ""
					error 149
					}
				else {
					}

		local counter = `counter' + 1
		di `counter'

		scalar coeff_`var' = _b[`var']
		//noi display coeff_`var'

		scalar coeff_se_`var' = _se[`var']
		//noi display coeff_se_`var'

		count if e(sample) == 1 & `var' == 1 //Count one tmt group at the time
		scalar obs_`var' = r(N)
		//noi display obs_`var'

	}

	local count: word count `varlist'
	local graphCount = `count' + 1
	//tokenize "`varlist'"
	//noi di `count'

	//Make all vars tempvars (maybe do later)
	//Make sure that missing is properly handled
	tempvar anyTMT
	egen `anyTMT' = rowmax(`varlist') if e(sample) == 1
	tempvar control
	gen `control' = (`anyTMT' == 0) if !missing(`anyTMT')

	sum `e(depvar)' if e(sample) == 1 & `control' == 1
	scalar ctl_N		= r(N)
	scalar ctl_mean	  	= r(mean)
	scalar ctl_mean_sd 	= r(sd)

		foreach var of local varlist {
			scalar  conf_int_min_`var'   =	coeff_`var'-(1.96*coeff_se_`var') + ctl_mean
			scalar  conf_int_max_`var'   =	coeff_`var'+(1.96*coeff_se_`var') + ctl_mean

			test `var'
			local star_`var' " "

			scalar  pvalue =r(p)
			if pvalue < 0.10 {
				local star_`var' "*"
			}
			if pvalue < 0.05 {
				local star_`var' "**"
			}
			if pvalue < 0.01 {
				local star_`var' "***"
			}
			*noi display "`star_`var''"
			scalar tmt_mean_`var' = ctl_mean + coeff_`var'
		}

	tempname 	newHandle
	tempfile	newTextFile
	cap file close 	`newHandle'
	file open  	`newHandle' using "`newTextFile'", text write append

	*Write headers and control value
	file write `newHandle' ///
		"order" 	_tab "xLabeled"	_tab "mean" _tab "coeff" _tab "conf_int_min" _tab "conf_int_max" _tab "obs" _tab "star" _n 	///
		%9.3f (1) _tab "Control"  _tab %9.3f (ctl_mean)      _tab  		 			_tab 		 			  	  _tab 			 				_tab %9.3f (ctl_N) 	   _tab			  	  _n

	tempvar newCounter
	gen `newCounter' = 2  //First tmt group starts at 2 (1 is control)

	foreach var in `varlist' {

		file write `newHandle' ///
			%9.3f (`newCounter') _tab "`var'" _tab %9.3f (tmt_mean_`var') _tab  %9.3f (coeff_`var')  _tab %9.3f (conf_int_min_`var') _tab %9.3f (conf_int_max_`var') _tab %9.3f (obs_`var')   _tab "`star_`var''"  _n

		replace `newCounter' = `newCounter' + 1
	}

	file close `newHandle'

	copy "`newTextFile'"  "mainMasterDofile.txt" , replace

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

		Create the graph

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

	insheet using mainMasterDofile.txt, clear

	local tmtGroupBars 	""
	local xAxisLabels 	`"xlabel( "'
	local legendLabels	`"lab(1 "`obsLabelControl'")"'
	local legendNumbers	""

	forval tmtGroupCount = 1/`graphCount' {

		************
		*Create the bar for this group

		colorPicker `tmtGroupCount' `graphCount'

		local tmtGroupBars `"`tmtGroupBars' (bar mean order if order == `tmtGroupCount', color("`r(color)'")) "'

		************
		*Create labels etc. for this group

		local obs 			= obs[`tmtGroupCount']
		local stars 		= star[`tmtGroupCount']
		local legendLabel 	= xlabeled[`tmtGroupCount']

		local xAxisLabels 	`"`xAxisLabels' `tmtGroupCount' "(N = `obs') `stars'" "'
		local legendLabels 	`"`legendLabels' lab(`tmtGroupCount' "`legendLabel'") "'
		local legendNumbers	`"`legendNumbers' `tmtGroupCount'"'
	}

	*Close or comple some strings
	local xAxisLabels `"`xAxisLabels' ,noticks labsize(medsmall)) "'
	local legendOption `"legend(order(`legendNumbers') `legendLabels')"'

	*local graphname gph_new

	*Create the confidence interval bars
	noi di "`CONFINT_BAR'"
	if `CONFINT_BAR' == 0 {
	//local confIntGraph == `"(scatter mean order,  msym(none)  mlabs(medium) mlabpos(10) mlabcolor(black)), xtitle("") ytitle("`e(depvar)'") "'
	local confIntGraph = `", xtitle("") ytitle("`e(depvar)'") "'
	}
	else if `CONFINT_BAR' == 1 {
	local confIntGraph = `"(rcap conf_int_max conf_int_min order, lc(gs)) (scatter mean order,  msym(none)  mlabs(medium) mlabpos(10) mlabcolor(black)), xtitle("") ytitle("`e(depvar)'")  "'
	}

	if "`save'" != "" {
		local saveOption saving("`save'", replace)
	}

	noi di `" graph twoway `tmtGroupBars' `confIntGraph' `legendOption' `xAxisLabels' `saveOption' title("`title'")   "'
	graph twoway `tmtGroupBars' `confIntGraph' `legendOption' `xAxisLabels' `saveOption' title("`title'")
	//noi di 	`"graph twoway `tmtGroupBars' `legendOption' (scatter mean order,  msym(none)  mlabs(medium) mlabpos(10) mlabcolor(black))), xtitle("") ytitle("`e(depvar)'") `xAxisLabels' `saveOption' title("`title'")  "'

	restore
}

end

cap	program drop	colorPicker
	program define 	colorPicker , rclass

	args groupCount totalNumGroups

	if `totalNumGroups' == 2 {

		if `groupCount' == 1 return local color "215 25 28"
		if `groupCount' == 2 return local color "43 123 182"
	}
	else if `totalNumGroups' == 3 {

		if `groupCount' == 1 return local color "215 25 28"
		if `groupCount' == 2 return local color "255 255 191"
		if `groupCount' == 3 return local color "43 123 182"
	}
	else if `totalNumGroups' == 4 {

		if `groupCount' == 1 return local color "215 25 28"
		if `groupCount' == 2 return local color "255 255 191"
		if `groupCount' == 3 return local color "171 217 233"
		if `groupCount' == 4 return local color "43 123 182"
	}
	else {

		*For five or more colors we repeat the same pattern

		local colourNum = mod(`groupCount', 5)
		if `colourNum' == 1 return local color "215 25 28"
		if `colourNum' == 2 return local color "253 174 93"
		if `colourNum' == 3 return local color "255 255 191"
		if `colourNum' == 4 return local color "171 217 233"
		if `colourNum' == 0 return local color "43 123 182"

	}

end


Bump help file version number

Bump help file version number

{smcl}
{* *! version 1.0.1 Matthew White 09mar2015}{...}
{title:Title}

{phang}
{cmd:stgit} {hline 2} Retrieve information about a Git repository

{marker syntax}{...}
{title:Syntax}

{p 8 10 2}
{cmd:stgit} [{cmd:status}]{cmd:,} [{it:options}]

{* Using -help odbc- as a template.}{...}
{* 26 is the position of the last character in the first column + 3.}{...}
{synoptset 26 tabbed}{...}
{synopthdr}
{synoptline}
{synopt:{opt git_dir(directory_name)}}GIT_DIR, the directory containing
the repository metadata{p_end}
{synoptline}
{p2colreset}{...}

{marker description}{...}
{title:Description}

{pstd}
{cmd:stgit} retrieves information about a Git repository,
including the current branch and the SHA-1 hash of the current commit.
In Stata 13 and above, it uses
the Java library {browse "https://eclipse.org/jgit/":JGit} to return
the status of the working tree.

{marker remarks}{...}
{title:Remarks}

{pstd}
In Stata 13 and above, {cmd:stgit} requires
the {help SSC} package {cmd:statajava}.
You must also {browse "https://eclipse.org/jgit/download/":download}
the JGit {cmd:.jar} file. (On the download page, it may be called
the "raw API library.") Place the {cmd:.jar} file on your {help ado-path}.
For instance, you may save it in your {help PERSONAL} system directory.
On your computer, this is {cmd}{ccl sysdir_personal}{txt}.

{pstd}
{cmd:stgit} respects {help version:version control} when deciding
which values to retrieve. Even in Stata 13 and above,
if the command interpreter is set to before version 13,
{cmd:stgit} does not use JGit, and returns limited information.

{pstd}
One use of {cmd:stgit} is adding commit hashes to dataset {help notes} and
to exported results such as tables. This facilitates reproducible research,
pinpointing the code that produced the files.

{pstd}
The GitHub repository for {cmd:stgit} is
{browse "https://github.com/matthew-white/stgit":here}.

{marker options}{...}
{title:Options}

{phang}
{opt git_dir()} specifies the path of GIT_DIR,
the repository metadata directory. This is typically named {cmd:.git}.
If {cmd:git_dir()} is not specified, {cmd:stgit} attempts to find GIT_DIR;
it assumes that the current working directory is within the Git repository.
Whether or not {cmd:git_dir()} is specified, {cmd:stgit} stores
the absolute path of GIT_DIR in {cmd:r(git_dir)}.

{marker examples}{...}
{title:Examples}

{pstd}Retrieve information about the repository within which
the current working directory is located{p_end}
{phang2}{cmd:stgit}

{pstd}Same as the previous {cmd:stgit} command{p_end}
{phang2}{cmd:stgit status}

{pstd}Retrieve information about the repository whose GIT_DIR is
{cmd:GitHub/cfout/.git}{p_end}
{phang2}{cmd:stgit, git_dir("GitHub/cfout/.git")}{p_end}

{marker results}{...}
{title:Stored results}

{pstd}
In Stata 9 and above, {cmd:stgit} stores the following in {cmd:r()}:

{synoptset 23 tabbed}{...}
{p2col 5 23 27 2: Scalars}{p_end}
{synopt:{cmd:r(has_detached_head)}}{cmd:1} if HEAD is detached,
{cmd:0} if not{p_end}

{p2col 5 23 27 2: Macros}{p_end}
{synopt:{cmd:r(git_dir)}}absolute path of GIT_DIR{p_end}
{synopt:{cmd:r(sha)}}SHA-1 hash of current commit{p_end}
{synopt:{cmd:r(branch)}}name of current branch or
{cmd:r(sha)} if HEAD is detached{p_end}
{p2colreset}{...}

{pstd}
In Stata 13 and above, {cmd:stgit} also stores the following in {cmd:r()}.
{cmd:stgit} retrieves these values using
the JGit class {cmd:org.eclipse.jgit.api.Status}, and much of
the language below is copied from
that class's {browse "https://eclipse.org/jgit/documentation/":API}
(version 3.5.3).

{synoptset 29 tabbed}{...}
{p2col 5 29 33 2: Scalars}{p_end}
{synopt:{cmd:r(is_clean)}}{cmd:1} if no differences exist between
the working tree, the index, and the current HEAD,
{cmd:0} if differences do exist{p_end}
{synopt:{cmd:r(has_uncommitted_changes)}}{cmd:1} if any tracked file is changed,
{cmd:0} if not{p_end}

{synoptset 26 tabbed}{...}
{p2col 5 26 30 2: Macros}{p_end}
{synopt:{cmd:r(untracked)}}list of files that are not ignored and not in
the index (e.g., what you get if you create a new file without adding it to
the index){p_end}
{synopt:{cmd:r(untracked_folders)}}list of directories that are not ignored and
not in the index{p_end}
{synopt:{cmd:r(uncommitted_changes)}}list of files and folders that are known to
the repository and changed either in the index or in the working tree{p_end}
{synopt:{cmd:r(added)}}list of files added to the index, not in HEAD (e.g.,
what you get if you call {cmd:git add ...} on a newly created file){p_end}
{synopt:{cmd:r(modified)}}list of files modified on disk relative to
the index (e.g., what you get if you modify an existing file without adding
it to the index){p_end}
{synopt:{cmd:r(changed)}}list of files changed from HEAD to index (e.g.,
what you get if you modify an existing file and call {cmd:git add ...} on
it){p_end}
{synopt:{cmd:r(missing)}}list of files in index but not filesystem (e.g.,
what you get if you call {cmd:rm ...} on an existing file){p_end}
{synopt:{cmd:r(removed)}}list of files removed from index, but in HEAD (e.g.,
what you get if you call {cmd:git rm ...} on an existing file){p_end}
{synopt:{cmd:r(conflicting)}}list of files that are in conflict (e.g.,
what you get if you modify a file that was modified by someone else in
the meantime){p_end}
{synopt:{cmd:r(ignored_not_in_index)}}list of files and folders that are
ignored and not in the index{p_end}
{p2colreset}{...}

{marker author}{...}
{title:Author}

{pstd}Matthew White{p_end}

{pstd}For questions or suggestions, submit a
{browse "https://github.com/matthew-white/stgit/issues":GitHub issue}
or e-mail researchsupport@poverty-action.org.{p_end}

{title:Also see}

{psee}
User-written:  {helpb git}
{p_end}

{smcl}
{* *! version 1.0.0 Matthew White 30dec2014}{...}
{title:Title}

{phang}
{cmd:stgit} {hline 2} Retrieve information about a Git repository

{marker syntax}{...}
{title:Syntax}

{p 8 10 2}
{cmd:stgit} [{cmd:status}]{cmd:,} [{it:options}]

{* Using -help odbc- as a template.}{...}
{* 26 is the position of the last character in the first column + 3.}{...}
{synoptset 26 tabbed}{...}
{synopthdr}
{synoptline}
{synopt:{opt git_dir(directory_name)}}GIT_DIR, the directory containing
the repository metadata{p_end}
{synoptline}
{p2colreset}{...}

{marker description}{...}
{title:Description}

{pstd}
{cmd:stgit} retrieves information about a Git repository,
including the current branch and the SHA-1 hash of the current commit.
In Stata 13 and above, it uses
the Java library {browse "https://eclipse.org/jgit/":JGit} to return
the status of the working tree.

{marker remarks}{...}
{title:Remarks}

{pstd}
In Stata 13 and above, {cmd:stgit} requires
the {help SSC} package {cmd:statajava}.
You must also {browse "https://eclipse.org/jgit/download/":download}
the JGit {cmd:.jar} file. (On the download page, it may be called
the "raw API library.") Place the {cmd:.jar} file on your {help ado-path}.
For instance, you may save it in your {help PERSONAL} system directory.
On your computer, this is {cmd}{ccl sysdir_personal}{txt}.

{pstd}
{cmd:stgit} respects {help version:version control} when deciding
which values to retrieve. Even in Stata 13 and above,
if the command interpreter is set to before version 13,
{cmd:stgit} does not use JGit, and returns limited information.

{pstd}
One use of {cmd:stgit} is adding commit hashes to dataset {help notes} and
to exported results such as tables. This facilitates reproducible research,
pinpointing the code that produced the files.

{pstd}
The GitHub repository for {cmd:stgit} is
{browse "https://github.com/matthew-white/stgit":here}.

{marker options}{...}
{title:Options}

{phang}
{opt git_dir()} specifies the path of GIT_DIR,
the repository metadata directory. This is typically named {cmd:.git}.
If {cmd:git_dir()} is not specified, {cmd:stgit} attempts to find GIT_DIR;
it assumes that the current working directory is within the Git repository.
Whether or not {cmd:git_dir()} is specified, {cmd:stgit} stores
the absolute path of GIT_DIR in {cmd:r(git_dir)}.

{marker examples}{...}
{title:Examples}

{pstd}Retrieve information about the repository within which
the current working directory is located{p_end}
{phang2}{cmd:stgit}

{pstd}Same as the previous {cmd:stgit} command{p_end}
{phang2}{cmd:stgit status}

{pstd}Retrieve information about the repository whose GIT_DIR is
{cmd:GitHub/cfout/.git}{p_end}
{phang2}{cmd:stgit, git_dir("GitHub/cfout/.git")}{p_end}

{marker results}{...}
{title:Stored results}

{pstd}
In Stata 9 and above, {cmd:stgit} stores the following in {cmd:r()}:

{synoptset 23 tabbed}{...}
{p2col 5 23 27 2: Scalars}{p_end}
{synopt:{cmd:r(has_detached_head)}}{cmd:1} if HEAD is detached,
{cmd:0} if not{p_end}

{p2col 5 23 27 2: Macros}{p_end}
{synopt:{cmd:r(git_dir)}}absolute path of GIT_DIR{p_end}
{synopt:{cmd:r(sha)}}SHA-1 hash of current commit{p_end}
{synopt:{cmd:r(branch)}}name of current branch or
{cmd:r(sha)} if HEAD is detached{p_end}
{p2colreset}{...}

{pstd}
In Stata 13 and above, {cmd:stgit} also stores the following in {cmd:r()}.
{cmd:stgit} retrieves these values using
the JGit class {cmd:org.eclipse.jgit.api.Status}, and much of
the language below is copied from
that class's {browse "https://eclipse.org/jgit/documentation/":API}
(version 3.5.3).

{synoptset 29 tabbed}{...}
{p2col 5 29 33 2: Scalars}{p_end}
{synopt:{cmd:r(is_clean)}}{cmd:1} if no differences exist between
the working tree, the index, and the current HEAD,
{cmd:0} if differences do exist{p_end}
{synopt:{cmd:r(has_uncommitted_changes)}}{cmd:1} if any tracked file is changed,
{cmd:0} if not{p_end}

{synoptset 26 tabbed}{...}
{p2col 5 26 30 2: Macros}{p_end}
{synopt:{cmd:r(untracked)}}list of files that are not ignored and not in
the index (e.g., what you get if you create a new file without adding it to
the index){p_end}
{synopt:{cmd:r(untracked_folders)}}list of directories that are not ignored and
not in the index{p_end}
{synopt:{cmd:r(uncommitted_changes)}}list of files and folders that are known to
the repository and changed either in the index or in the working tree{p_end}
{synopt:{cmd:r(added)}}list of files added to the index, not in HEAD (e.g.,
what you get if you call {cmd:git add ...} on a newly created file){p_end}
{synopt:{cmd:r(modified)}}list of files modified on disk relative to
the index (e.g., what you get if you modify an existing file without adding
it to the index){p_end}
{synopt:{cmd:r(changed)}}list of files changed from HEAD to index (e.g.,
what you get if you modify an existing file and call {cmd:git add ...} on
it){p_end}
{synopt:{cmd:r(missing)}}list of files in index but not filesystem (e.g.,
what you get if you call {cmd:rm ...} on an existing file){p_end}
{synopt:{cmd:r(removed)}}list of files removed from index, but in HEAD (e.g.,
what you get if you call {cmd:git rm ...} on an existing file){p_end}
{synopt:{cmd:r(conflicting)}}list of files that are in conflict (e.g.,
what you get if you modify a file that was modified by someone else in
the meantime){p_end}
{synopt:{cmd:r(ignored_not_in_index)}}list of files and folders that are
ignored and not in the index{p_end}
{p2colreset}{...}

{marker author}{...}
{title:Author}

{pstd}Matthew White{p_end}

{pstd}For questions or suggestions, submit a
{browse "https://github.com/matthew-white/stgit/issues":GitHub issue}
or e-mail researchsupport@poverty-action.org.{p_end}

{title:Also see}

{psee}
User-written:  {helpb git}
{p_end}