* user = your-id
* password = your-password
* package = stata
* project = lis

#delimit;
global keepit "casenum hweight d4 d5 dpi";

program define setups;
*** avoid double counting of household records;
drop if d5==3;
*** select only records if DPI filled;
drop if dpi==.;
drop if dpi==0;
*** set equivalence scale as square root of number of persons;
generate ey=(dpi/(d4^0.5));
*** standardise weights so that average weight = 1 ;
qui sum hweight, detail;
gen nwgt=hweight/_result(3);
generate pweight=(nwgt*d4);
end;
*** bottomcoding at one percent equivalized mean;
program define bottom;
setups;
quietly sum ey [w=pweight];
generate botlin=0.01*_result(3);
replace ey=botlin if ey<botlin;
end;
*** topcoding at ten times unequivalized median;
program define top;
quietly sum dpi [w=pweight], de;
generate toplin=10*_result(10);
replace ey=(toplin/(d4^0.5)) if dpi>toplin;
end;

use $keepit using $ccyyh;
bottom;
top;
set seed 100001;
version 7 : bs "ineqdeco ey [aw=pweight]" "S_gini", reps(500);