I am looping over a variable, running a regression, and extracting the coefficient for each level (to plot on a twoway graph). I created a new variable to collapse later (maybe not most efficient but easy) and store the values in there. But I am struggling with the fact that the value in the local of levelsof is different to the level of the variable.

This MWE does not work but should illustrate what I am attempting to do

Code:
sysuse auto.dta, replace

gen corr = .

levelsof foreign, local(foreign)
foreach f in `foreign' {
    reg price weight
    mat result = e(b)
    replace corr = result[1,1] if foreign == `f'
}