program define xtdiff
version 7.0

	local iis : char _dta[iis]
	local tis : char _dta[tis]

	if ("`iis'" == "" | "`tis'" == "") {
		display " Wrong! You need to set iis and tis" }
	error ("`iis'" == ""  | "`tis'" == "")

	args x xd

	if ("`xd'" == "") {
		display " Wrong! Specify a stem for difference variables"
		}
	error ("`xd'" == "")

	sort `iis' `tis'

	tempvar T
	tempvar Tmax

	by `iis' : gen `T' = _N
	egen `Tmax' = max(`T')
	local Tmax0 = `Tmax'[1]
	local Tmax1 = `Tmax0'-1

	forvalues lagf = 1(1)`Tmax1' {
		quietly by `iis' : gen `xd'`lagf' = `x'[_n+`lagf'] - `x'
	}

	quietly compress `xd'1-`xd'`Tmax1'

end