prog define ptok, rclass
*!	*	Ver.  1.01 14Aug2023  Subroutine for -diffplot-
*	Check the period and treated variables to make sure they conform to -diff- conventions
*	Add default labels if they are missing
	version 14
	syntax , period(varname fv numeric) treated(varname fv numeric)   ///
		[trtlbl(string) conlbl(string) beflbl(string) aftlbl(string) verbose ]

	//	Extract portion of variable names that follows the "."
	local pname  = substr("`period'"  ,strpos("`period'",".") +1  ,.)
	local gname  = substr("`treated'" ,strpos("`treated'",".")+1 ,.)

	local p_in_g = strpos("`gname')","`pname'")
	local g_in_p = strpos("`pname')","`gname'")

	if `p_in_g'>0 {
		di as err "The variable name of the -period()- variable, -{it:`pname'}-,"  ///
			_n as err "is embedded in the variable name of the -treated()- variable, -{it:`gname'}-."  ///
			_n as err "Rename one of the two variables specified in options -period(`pname')- and -treated(`gname')-"  ///
			_n as err "so that neither name matches a character string within the other."
			exit 198
	}

	if `g_in_p'>0 {
		di as err "The variable name of the -treated()- variable, -{it:`gname'}-,"  ///
			_n as err "is embedded in the variable name of the -period()- variable, -{it:`pname'}-."  ///
			_n as err "Rename one of the two variables specified in options -period(`pname')- and -treated(`gname')-"  ///
			_n as err "so that neither name matches a character string within the other."
			exit 198
	}

	return add  //  Prevents -levelsof- from erasing r() from -diff-
	local shhh = cond("`verbose'"=="", "quietly", "")

*	Check and make labels for variable `period'
	qui levelsof `period'
		if `"`r(levels)'"'~=`"0 1"'  {
			di as err "The variable identified in the -period- option must have two integer values: (0: Before; 1: After)."  ///
				_n "Here the levels of -`period'- are:"  ///
				_n `"`r(levels)'"'
			exit 198
		}
		local periodval = 0
		foreach altlbl in beflbl aftlbl  {
			if `"``altlbl''"'=="" {
				local newlblname : label (`period') `periodval'
				if "`newlblname'"=="`periodval'"  {
					local newlblname  = cond("`altlbl'"=="beflbl","Before","After") // Default label
				}
			}
			else {
					local newlblname  = `"``altlbl''"'  // User selected label
			}
			local `altlbl' = subinstr(`"`newlblname'"'," ","_",.)
			local periodval = 1
		}

*	Check and make labels for variable `treated'
	qui levelsof `treated'
		if `"`r(levels)'"'~=`"0 1"'  {
			di as err "The variable identified in the -treated- option must have two integer values: (0: Control; 1: Treated)."  ///
				_n "Here the levels of -`treated'- are:"  ///
				_n `"`r(levels)'"'

			exit 198
		}

		local trtval = 0
		foreach altlbl in conlbl trtlbl  {
			if `"``altlbl''"'=="" {
				local newlblname : label (`treated') `trtval'
				if "`newlblname'"=="`trtval'"  {
					local newlblname  = cond("`altlbl'"=="conlbl","Control","Treated") // Default label
				}
			}
			else {
					local newlblname  = `"``altlbl''"'  // User selected label
			}
			local `altlbl' = subinstr(`"`newlblname'"'," ","_",.)
			local trtval = 1
		}

	return local beforelbl  `beflbl'
	return local afterlbl   `aftlbl'
	return local controllbl `conlbl'
	return local treatedlbl `trtlbl'

end  /* End of program ptok  */
*	Ver.1.01 14Aug2023