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!
Related Posts with Variable not found (after others created in the same way)
How to extract numbers and operation symbols from string variables in StataHi all, I have a question about how to extract numbers and operation symbols from a string variable…
Help Building a Loop to tell me which observations meet my CriterionHello wonderful people of statlist. I have a question that is based off of an offshoot of a prior qu…
Plotting polynomial regressionHi there, I am trying to plot a polynomial regression between the variables hiscam and my third ord…
Probit versus marginsDear all, I am testing the effect of a policy using a DID strategy, I get positive and significant …
how to rescale data based on their cross-sectional average of each yearHello everyone, I hope you are all in good health Please bear with me, I am not really familiar with…
Subscribe to:
Post Comments (Atom)
0 Response to Variable not found (after others created in the same way)
Post a Comment