Announcement

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

  • Ambiguous Abbreviation Error in Loop

    Hi.

    I have this problem I have been trying to solve to no avail. Basically, I'm running these loops that need to have the variables as local macros. I keep getting an ambiguous abbreviation error that I can't figure out.


    Variables in my data
    elast
    coef_var1 through coef_var5

    There are other variables in my data like coef_se_var1 through 5, as well.


    So I first define my macros:
    local elastvar elast_`i'
    local coef coef_var`i'
    local sumelast elast_*


    Then, I run a loop that iterates through each of the 1 through 5 suffixes:

    foreach i in 1 2 3 4 5 {
    gen elastvar = 0
    replace `elastvar' = (`coef'*avg)/avgprice if intx_var`i'=="NA"
    replace `elastvar' = (`coef'*avg*size)/price if intx_var`i'=="area"
    replace `elastvar' = (`coef'*avg*dist)/price if intx_var`i'=="linear"
    replace `elastvar'= 0 if `elastvar'==.
    }


    I get an error that reads coef_var ambiguous abbreviation. Does anyone have any suggestions?

  • #2
    Hello Lane,

    Welcome to Statalist.

    Your issue stems from the fact that you defined the local macros outside of your foreach loop, presumably.

    When you do
    Code:
    local coef coef_var`i'
    the moment you define the local coef, Stata looks to see what the local i holds. Well, because you defined coef before defining i, Stata sees that i doesn't equal anything, so your local coef ends up being just "coef_var".

    What you need is to define local coef within your foreach loop. But why even make a local coef? Instead, just integrate your local i straight into your code. Example:
    Code:
    foreach i in 1 2 3 4 5 { //This line can be better written as the following: forvalues i=1/5 {
    gen elast_`i' = 0
    replace elast_`i' = (coef_var`i'*avg)/avgprice if intx_var`i'=="NA"
    replace elast_`i' = (coef_var`i'*avg*size)/price if intx_var`i'=="area"
    replace elast_`i' = (coef_var`i'*avg*dist)/price if intx_var`i'=="linear"
    replace elast_`i'= 0 if elast_`i'==.
    }
    Alternatively, if you are bent on defining the locals elastvar and coef, you must use backslash to tell Stata to wait until you call elastvar or coef to evaluate what `i' is. Example:
    Code:
    local elastvar elast_\`i\'
    local coef coef_var\`i\'

    Comment


    • #3
      Roger gives excellent advice. I'll add the tweak that the line

      Code:
       
       replace elast_`i'= 0 if elast_`i'==.
      looks unnecessary, as it is hard to see how the variable could ever be missing unless prices are zero.

      Alternatively, should all those zero estimates of elasticity (if that is what this is) perhaps be missing instead!

      Comment


      • #4
        Thanks for the responses! I was able to get it to work with your advice. The reason why I want to define my locals outside of the loop is because I want to be able to repeat the loop with different things defined as elast_var and coef_var. I'm going to have to run this same loop for a bunch different variables, so I wanted to just have to redefine the local macros, rather than recreating the loop and changing the variable names in each loop every time.

        Using the \`i\' notation when I define my loops outside the code fixed it!

        To Nick's point, something that I left out of my code that I posted was that I later add all my individual elasticity variables, which is why I changed the missings to zero! In reality, those elasticities are missing, but for the purpose of my code, I have them at zero.

        Again, thank you both for your responses. Now the problem that has totally stumped me is solved, and I can move on in my analysis!

        Comment


        • #5
          Thanks for the extra information. I still see no reason for the delayed evaluation. There are all sorts of ways of generalising the code without resorting to that, but quite how depends naturally on the details.

          Comment


          • #6
            input double(a_PaddyCultivation a_WheatCultivation a_OtherGrainCultivation Supply)
            66921.282579851 0 0 1341642.553549195
            0 7531.183290549666 0 171103.50329370794
            0 0 10377.952636518654 119773.41411254935
            0 0 0 64790.35205861267
            0 0 0 23983.478305916753
            0 0 0 250956.23910320186
            0 0 0 172873.70279778395
            0 0 0 102269.45208124879
            0 0 0 205353.97487719177
            0 0 0 290889.62485367316
            0 0 0 232335.2226671952
            0 0 0 15816.188217939856
            0 0 0 19820.560764739374
            0 0 0 330000.0571648817
            0 0 0 240.7973682088578
            0 0 0 20000.6301194368
            81302.55742910138 5259.091537932892 0 587238.5291317557
            0 14503.146261266822 106295.46147603073 688184.8279557704
            0 0 0 437640.36583824863
            0 0 0 917958.2961106062
            0 0 0 205683.7049409231
            0 0 0 638738.0637132773
            1065793.176503222 0 0 1715701.4967092494
            0 35263.80812938767 0 107246.87206164491
            0 0 0 17389.188214568167
            0 0 0 258678.4672574274
            0 0 0 176247.0796438689
            0 0 0 8425.53084580912
            0 0 0 6012.576910405995
            18.24014853689008 325.6951076696727 0 8703.579017402839
            649.4576745234085 11596.68117849996 0 116042.28530442058
            5254.4904472625085 93823.89778840123 0 777040.6361841979

            above is the short extract of my data set;

            I am using following loop and I get error "a ambiguous abbreviation" which I cannot figure out why. Your help will be appreciated.

            foreach var in a* {
            replace `var'=`var'/supply'
            }

            Comment


            • #7
              foreach has two syntaxes which are quite different and can't be mixed.

              in a* is legal in itself but foreach won't unpack the varlist for you. So, your first (and only) command implied by the loop is


              Code:
              replace a*= a*/supply'
              and Stata stops at the first a* well before it notices the stray punctuation at the end of the line.

              You need of var a* (and to fix the typo).

              Comment

              Working...
              X