- * get sum of var
- su var
- local sum=r(mean)*r(N)
- * gen var2 randomly - distribution does not matter
- * uniform makes sure there is no negative number
- gen var2=runiform()
- * get the two sums to be the same
- su var2
- replace var2=var2/(r(mean)*r(N))*`sum'
- * round var2
- replace var2=round(var2)
- * check by how much the two sums differ
- su var2
- local diff=`sum'-r(mean)*r(N)
- * randomly choose a few observations of var2 to apply correction
- gen var3=runiform() if var2>=1
- sort var3
- replace var2=var2+1 in 1/`diff'
- drop var3
- * verify the two means(i.e., sums) are exactly equal
- su var var2
第二个问题就很简单了
gen id=_n
preserve
gen i=rnormal()
sort i
replace id=_n
rename var var2
drop i
save tmp, replace
restore
merge 1:1 id using tmp, nogenerate
drop id
erase tmp.dta