Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Thank YOU RE: best way to create iweight - code?


From   lisa jahns <jahns@email.unc.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Thank YOU RE: best way to create iweight - code?
Date   Fri, 14 Nov 2003 12:20:24 -0500

Thanks to everyone who helped me to figure this out - and especially to Michael Blasnick for the very useful code - it works wonderfully!

Lisa

I have some code that I've used to do this and just updated it to v8.2.
It's short enough that I'm attaching it below, but you may find line
wrapping problems. The longest line is the one to label the new variable.

To apply this program to your problem you will need to append the datasets
together and have some identifier for the dataset that represents the
population you want to match to. For example:

use datasetA
keep age gender
gen byte pop=1
append using datasetB
replace pop=0 if pop==.
matchwt age gender , match(pop) gen(myweight)
drop if pop==1
drop pop

This will create a new variable in datasetB called myweight that will
contain the weights that you want (I hope). Unlike the manual cross-tab
approach, there is no real technical limit to the number of variables you
can match on. You may want to first bin the age variable (e.g.,
0-5,6-10,etc) to lower the likelihood of no matches in the population,
unless you have a large datasetA

program define matchwt
version 8.2
*! M Blasnik Nov-03 v 2.0
syntax varlist [if] , match(str) gen(str)
tempvar binn pop binnP
tempname counts
marksample touse, strok
quietly {
gen byte `pop'=cond(`match',1,0) if `touse'
tab `pop' if `touse', matcell(`counts')
local popn=`counts'[2,1]
local sampn=`counts'[1,1]
bys `touse' `varlist' `pop': gen `binn'=_N if `touse'
by `touse' `varlist' (`pop'): gen `binnP'=`binn'[_N]*`pop'[_N]
}
gen `gen'=cond(`pop'==1,1,(`binnP'/`popn')/(`binn'/`sampn'))
label var `gen' "weights to match cases where `match' `if', based on
`varlist'"
end

Michael Blasnik
michael.blasnik@verizon.net


De : owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu]De <mailto:owner-statalist@hsphsun2.harvard.edu%5DDe>
<mailto:owner-statalist@hsphsun2.harvard.edu%5DDe> la part de Lisa Jahns
Envoyé : jeudi 13 novembre 2003 15:59
À : statalist@hsphsun2.harvard.edu
Objet : st: best way to create iweight?


I have dataset "A" with some distribution of age (in years) and sex
(0,1), and totally unrelated dataset "B" with some different
distribution of age and sex.  What is the most parsimonious way to
create an iweight for individuals in dataset B that will give it the
same age and sex distribution of dataset A?

Thank you,
Lisa Jahns
University of North Carolina


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index