***
*** Example 3.4. Welfare measures for South Africa (Table 3.4)
***
*** The code is similar to that for Côte d'Ivoire but is adapted 
*** to the South African data.
*** 
*** Note: The following provides the STATA code used to produce 
*** results used in "The Analysis of Household Surveys: 
*** A Microeconometric Approach to Development Policy", by Angus Deaton.
*** This book, published for the World Bank by The Johns Hopkins University
*** Press and scheduled for release in 1997.
***
version 4.0
#delimit ;
drop _all;
cap log close;
log using sastats, replace;
/* household roster file */
use c:\safrica\work\m6_hrost;
/* keeping only members */
keep if mcode==1;
drop if gender_n==1;
drop if age==.;
drop if age <= 0;
keep hhid;
sort hhid;
qui by hhid: gen hhs=_N;
qui by hhid: keep if _n==1;
/* picking up the weights */
merge hhid using c:\safrica\work\strata2;
tab _m; drop _m;
keep hhid hhs race rsweight;
sort hhid;
/* picking up household expenditures */
merge hhid using c:\safrica\work\hhexptl;
keep hhid hhs race rsweight totmexp;
gen pce=totmexp/hhs;
gen wt=rsweight*hhs;
gen clus=int(hhid/1000);
sort race;
summ totmexp hhs pce [aweight=rsweight];
summ pce [aweight=wt];
by race: summ totmexp hhs pce [aweight=rsweight];
by race: summ pce [aweight=wt];
varset pweight wt;
varset psu clus;
svymean pce;
svymean pce, subpop(race==1);
svymean pce, subpop(race==2);
svymean pce, subpop(race==3);
svymean pce, subpop(race==4); 
varset pweight rsweight;
svyratio totmexp/hhs;
svyratio totmexp/hhs, subpop(race==1);
svyratio totmexp/hhs, subpop(race==2);
svyratio totmexp/hhs, subpop(race==3);
svyratio totmexp/hhs, subpop(race==4);
save sastats, replace;
drop _all;
cap program drop ginisa;
program def ginisa;
local rg=0;
while `rg' <= 4 {;
drop _all;
use sastats;
if `rg' > 0 {;keep if race==`rg';};
giniw;
dis("Gini coefficient race =`rg' =$g");
local rg=`rg'+1;
};
end; 
cap program drop abasa;
program def abasa;
local rg=0;
while `rg' <= 4 {;
drop _all;
use sastats;
if `rg' > 0 {;keep if race==`rg';};
qui summ pce [aweight=wt];
local mn=_result(3);
gen pcerat=pce/`mn';
if `1' ==1 {;
gen tm=ln(pce/`mn');
qui summ tm [aweight=wt];
local ineq=1-exp(_result(3));};
else {;
gen tm=(pce/`mn')^`1';
qui summ tm [aweight=wt];
local ineq=1-(_result(3))^(1/(1-`1'));};
dis("race `rg' Atkinson coefficient, eps=`1' =`ineq'");
local rg=`rg'+1;
};
end; 
cap program drop sdlogssa;
program define sdlogssa;
local rg=0;
while `rg' <= 4 {;
drop _all;
use sastats;
if `rg' > 0 {;keep if race==`rg';};
gen lnpce=ln(pce);
qui summ lnpce [aweight=wt];
local sdln=sqrt(_result(4));
dis("Race `rg' s.d. of lnpce, = `sdln'");
local rg=`rg'+1;
};
end; 
cap program drop coefvar;
program define coefvar;
local rg=0;
while `rg' <= 4 {;
drop _all;
use sastats;
if `rg' > 0 {;keep if race==`rg';};
qui summ pce [aweight=wt];
local cv=sqrt(_result(4))/_result(3); 
dis("Race `rg' c.o.v. of pce, = `cv'");
local rg=`rg'+1;
};
end; 
cap program drop peassa;
program def peassa;
local rg=0;
while `rg' <= 4 {;
drop _all;
use sastats;
if `rg' > 0 {;keep if race==`rg';};
gen pind=pce <= $pline;
qui summ pind [aweight=wt];
local p0=_result(3);
gen df=(1-pce/$pline)*pind;
qui summ df [aweight=wt];
local p1=_result(3);
gen dfa=(1-pce/$pline)^2*pind;
qui summ dfa [aweight=wt];
local p2=_result(3);
drop if pce > $pline;
if _N > 0 {;
giniw;};
else {;global g=1;};
local psen=`p0'*$g+`p1'*(1-$g);
dis("P0=`p0',P1=`p1',P2=`p2',Psen=`psen'");
drop _all;
local rg=`rg'+1;
};
end; 
cap program drop giniw;
program def giniw;
gsort - pce;
qui {;
gen hrnk=sum(wt);
gen hhrnk=hrnk[_n-1]+1;
replace hhrnk=1 in 1;};
qui summ wt;
local bign=_result(1)*_result(3);
qui summ pce [aweight=wt];
local mn=_result(3);
gen rnkx=pce*(hhrnk+0.5*(wt-1));
qui summ rnkx [aweight=wt];
global g=(`bign'+1)-(2/`mn')*_result(3);
global g=$g/(`bign'-1);
end; 
ginisa;
abasa 0.5;
abasa 1;
abasa 2;
sdlogssa;
coefvar;
global pline=105;
peassa; 
log close;

[Return to Program List]