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

st: Re: RE: RE: best way to create iweight - code?

From   "Michael Blasnik" <>
To   <>
Subject   st: Re: RE: RE: best way to create iweight - code?
Date   Thu, 13 Nov 2003 13:33:29 -0500

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

Michael Blasnik

> De :
> []De
<> la part de Lisa Jahns
> Envoyé : jeudi 13 novembre 2003 15:59
> À :
> 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:

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