x cd /pub/data/sf32000/Tools; %let pgm=calculate_gini; filename pgm "&pgm..sas"; ********This does not work (yet) ********************************************************************; *--Code to calculate the gini coefficient, a measure of income distribution inequality, using an sf3 data table as input. jgb, OSEDA/MCDC December, 2013. We use the code and explanation at http://www.lexjansen.com/wuss/1997/WUSS97044.pdf : "Using SAS Software to Estimate county level GINI Coefficients" by Daric Burmmett. We shall be using 2000 SF3 tables as input, whereas he was using 1989 income data apparently from a CBP source. --*; libname sf3 '/pub/data/sf32000'; *--The default values assigned in the macro defn will work with SF3 data sets 2000 census using the MCDC table variable naming conventions. ---*; %macro gini( setin=sf3.usregdiv, /*--input data set containing the specified tables--*/ setout=ginidata, /* saved output with just &idvars and single state variable gini */ idvars=geocode areaname, /* variables on input set kept to ID the obs. First var must be unique id */ hhitable=p52, /*--Household income distribution table--*/ hhitottable=p54, /*--3-cell table where the total hh income is reported, broken down by under and over the last (highest with "and over" upper limit) income category.--*/ nintervals=16, /* # of HH income intervals = 1 less than dimension of hhitable */ llimits=0 10000 15000 20000 25000 30000 35000 40000 45000 50000 60000 75000 100000 125000 150000 200000, /*--the lower limits of the income distribution table. We can calculate midpoints and use to estimate total income for each of the intervals. These value will then be "raked" to match the total income not in last interval.--*/ debug=0); %local i j k nintsm1 midv midvals tabledim; %let nintsm1=%eval(&nintervals -1); %let tabledim=%eval(&nintervals+1); %let midvals=; %*---calculate and save the midpoints of the income intervals----; %do i=1 %to &nintsm1; %if &i gt 1 %then %let low=&high; %else %let low=%scan(&llimits,&i); %let high=%scan(&llimits,%eval(&i+1)); %let midv=%eval( (&high + &low)/2 ); %let midvals=&midvals &midv; %end; %if &debug %then %put _all_; data gini; set &setin (keep=geocode areaname &hhitable.i: &hhitottable.i: ); TotHHs=&hhitable.i1; TotIncome=&hhitottable.i1; TotIncomeLastInterval=&hhitottable.i3; array &hhitable (&nintervals) &hhitable.i2 - &hhitable.i&tabledim ; array midptvals(&nintervals) _temporary_ (&midvals 0); /*<=========================Comment out===================================== array incomevals(&nintervals) _temporary_; array incomevals(&nintervals) _temporary_; array pctincomevals(&nintervals) _temporary_; array pcthhcounts(&nintervals) _temporary_ ; ===========================End commented out================================ */ *---First, we want to estimate the income in each interval by multiplying the HH count by the midpoint value. We sum these estimates to get a preliminary figure. Then go back and apply a "raking factor" to adjust these values so that they match the total provided in the &hhitottable cell 2 ---; _tempsum=0; do i=1 to &nintsm1; _tempsum + (&hhitable(i) * midptvals(i)); end; _incomeFactor=&hhitottable.i2/_tempsum; if &debug and _n_=1 then put _tempsum= _incomeFactor=; *---Now we calculate the total incomes and the cumulative percent incomes for the intervals; cumhhs=0; cumincome=0; do j=1 to &nintervals; if j=&nintervals then incomeThisinterval=&hhitottable.i3; else incomeThisinterval=(&hhitable(j) * midptvals(j))*_incomeFactor; cumhhs+&hhitable(j); cumpcthhs=cumhhs/TotHHs; cumincome+incomeThisinterval; cumpctinc=cumincome/TotIncome; *--From Brummets WUSS paper (p.3): "The final preparation step is to transpose data to vertical for PROC REG and perform log transfromations.". We follow his lead even if we do not comprehend. --; if round(cumpctinc,.00001)=1 then cumpctinc=.99999 ; if round(cumpcthhs,.00001)=1 then cumpcthhs=.99999 ; lpi=log(1-cumpctinc); lhh=log(1-cumpcthhs); output; if &debug and _n_=1 then put / _all_ ; end; keep &idvars lpi lhh; run; *---we want to calculate the pct and cumpct values of HH count and estimated total income for each cell in the HH income distribution table.---; proc sort; by &idvars lhh; *--Again, code borrowed directly fromBrummet paper--; *--Run regressions---; proc reg outest=parms(rename=(lhh=beta)) noprint; model lpi=lhh/noint; by &idvars; output out=est p=p; run; data &setout; set parms; GINI=(2/(1+beta)) -1; keep &idvars GINI; run; proc print data=&setout; id &idvars; title "&setout With GINI Coefficients"; run; %mend gini; data us; set sf3.usregdiv(obs=1); geocode='00'; run; options mprint; %gini(setin=us,debug=1) ;