capture program drop orderedid
program define orderedid
	syntax varname
	local oldid `1'
	sort `oldid'
	by `oldid': gen first=1 if _n==1
	gen id=sum(first)
	drop first
end