Announcement

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

  • Variable not found (after others created in the same way)

    I have the following nested for loops to run the same calculation for different radii, I have bolded the problematic line, since there's a lot of surrounding code, but since it's in a loop I thought it might be relevant:

    * Outside for loop runs HHI calculations for each radii of interest
    foreach radi in 50 30 20 10 {
    use ${SS}ICD_Chapter_distances_`radi'.dta, clear

    * This for loop calculates the HHI for each (4) relevant ICD chapter for each hospital using the current radii.
    foreach chapt of varlist Chapter_2_KH2 Chapter_9_KH2 Chapter_13_KH2 Chapter_19_KH2 {
    egen sum_`chapt' = sum(`chapt'), by (ID_count) /* Calculates total number of ICD chapter cases in market */
    gen prop1_`chapt' = (`chapt' / sum_`chapt')
    gen prop2_`chapt' = (prop1_`chapt')^2 /* Calculates the hospital's proportion of cases in it's market, then squares it */
    egen HHI_`chapt' = sum(prop2_`chapt'), by (ID_count) /* Sums squared proportions to give HHI */

    * Next we calculate the distance-weighted HHI
    gen distProp_`chapt' = distance / `radi' /* Calculates percentage of how far away hospital is out of the radius */
    gen weighted_`chapt' = `chapt' * (1 - distProp) /* Weights chapter cases by multiplying them by 1 minus the distance proportion */
    egen wSum_`chapt' = sum(weighted_`chapt'), by (ID_count) /* Sum these weighted cases to get distance-weighted market total */
    gen wProp_`chapt' = weighted_`chapt' / wSum_`chapt' /* Calculates weighted proportions */
    gen wProp2_`chapt' = (wProp_`chapt') ^ 2 /* Squares these proportions */
    egen weightedHHI_`chapt' = sum(wProp2_`chapt'), by (ID_count)

    }

    I receive the error "distProp" not found, which is strange to me since that shouldn't even be considered a complete variable name since I have the _`chapt' attached to it, just like I do when creating my other variables. The first calculation runs fine on it's own, so I don't know why stata can't figure out that "distProp_`chapt'" should become, for example: distProp_chapter_2_KH2, when it's able to do that earlier. I've tried changing it to dstPrp, distanceP, and whatever, all of which yield the same results. I've also tried initializing the variable to zero first and then replacing it, but that does not work either.

    Any help is quite appreciated, thank you!

  • #2
    I do not agree that

    gen distProp_`chapt' = distance / `radi' /*
    is the problematic line. This potentially could be it because you do explicitly refer to the variable

    gen weighted_`chapt' = `chapt' * (1 - distProp) /*
    To diagnose errors, you should have

    Code:
    set trace on

    Comment

    Working...
    X