Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • r(101): trying to advance in my thesis and I have encountered this error while doing a loop

    Hey guys!
    I have written a 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. Any help would be amazing!
    Thank you in advance!

    *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
    }



  • #2
    Hi Laura,

    note that it is very hard to help you if you don't have a reproducible code and/or a fraction of your data or any other similar data that we can access. Also please check the guidelines for formatting your question. Your code should be wrapped.

    Without being able to run your code, it seems that your problem is coming from this line:

    Code:
    if treated [`i']== 1 {
    In Stata squared brackets are frequently used for using the weights option. So you would need to drop that space.

    Code:
    if treated[`i']== 1 {
    More generally there would seem to be easier ways to do that kind of match. something like

    Code:
    merge 1:m sex childage treated using ...
    you can save your dataset and merge it with itself.

    Again its very hard to help you without having a sample of the data or a clear idea of what you are trying to achieve or why you are following such complicated approach.

    Good luck.

    Comment


    • #3
      Also cross-posted on Stack Overflow.

      Comment


      • #4
        There was one suggestion at https://stackoverflow.com/questions/...-error-while-d

        Where does the code fail?

        Comment


        • #5
          Thank you so much for your help

          Comment

          Working...
          X