Reputation: 1
I have written Stata code for an exact matching of children with mental health problems with children without mental health problems on sex (males, females) and age (3, 4 and 5 years of age). I want to do a 1:2 matching. I keep getting error messages such as "weights not allowed". What am I doing wrong? Below I have attached the code.
* Exact matching on age and sex*
* Sort data by mental health problem status, sex and age */
sort treated Sex ChildAge````
* Generate a matching ID variable with prefix for case/control */
gen _match_id = _n
local case_prefix "T_"
local control_prefix "C_"```` // Adjust prefix for your control group
* Loop through cases (children with mental health problems) */
foreach i in 1/`_N'{
if treated [`i']== 1 {
local match_found= 0
local j=`i'+ 1
local control_count= 0
while`match_found'<2&`j'<= `_N'{
if Sex[_N]==Sex[`j']&ChildAge[_n]==ChildAge[`j'] & treated[`j']==0{
replace _match_id[`j']= concat(`control_prefix', _match_id[_N])
local match_found= match_found+1
local control_count= control_count+1
}
local j=j+1
}
}
}
* Drop cases (children with mental health problems) with less than 2 matches */
if match_found < 2 & treated[_N] == 1 {
drop if _n == _N`
* Keep cases (children with mental health problems) and matched controls */
keep if _match_id != . | _match_id like "`case_prefix'%"````
* Drop the matching ID variable */
drop _match_id
}
I keep getting r(101) no weights allowed, but there are no explicit weights. I have also tried adjusting the } with no success.
Upvotes: 0
Views: 98
Reputation: 37288
foreach i in 1/`_N'
makes no sense to Stata. It should probably be
forval i = 1/`=_N'
Upvotes: 0