Announcement

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

  • Error message when using levelsof/local macro/foreach (variable not found)

    Hi all,

    I'm attempting to run the code below, but keep getting the following error message: [0.tteshift] not found

    generate coef = .
    generate se = .
    levelsof tteshift, local(times)

    foreach t of local times {
    replace coef = _b[`t'.tteshift] if tteshift == `t'
    replace se = _se[`t'.tteshift] if tteshift == `t'
    }

    ----

    For reference, "levelsof tteshift" produces the following output:
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

    Additionally:

    . summarize i.tteshift

    Variable | Obs Mean Std. dev. Min Max
    -------------+---------------------------------------------------------
    tteshift |
    0 | 1,326 .0007541 .0274618 0 1
    1 | 1,326 .0007541 .0274618 0 1
    2 | 1,326 .0007541 .0274618 0 1
    3 | 1,326 .0015083 .0388221 0 1
    4 | 1,326 .0022624 .0475292 0 1
    -------------+---------------------------------------------------------
    5 | 1,326 .0030166 .0548613 0 1
    6 | 1,326 .0060332 .0774681 0 1
    7 | 1,326 .0082956 .090736 0 1
    8 | 1,326 .0113122 .1057955 0 1
    9 | 1,326 .0120664 .1092235 0 1
    -------------+---------------------------------------------------------
    10 | 1,326 .0135747 .1157605 0 1
    11 | 1,326 .015083 .121929 0 1
    12 | 1,326 .0218703 .1463151 0 1
    13 | 1,326 .0316742 .1751973 0 1
    14 | 1,326 .0354449 .1849714 0 1
    -------------+---------------------------------------------------------
    15 | 1,326 .1915535 .393672 0 1
    16 | 1,326 .0505279 .2191142 0 1
    17 | 1,326 .0497738 .2175592 0 1
    18 | 1,326 .0497738 .2175592 0 1
    19 | 1,326 .0497738 .2175592 0 1
    -------------+---------------------------------------------------------
    20 | 1,326 .0490196 .2159904 0 1
    21 | 1,326 .0482655 .2144075 0 1
    22 | 1,326 .0475113 .2128101 0 1
    23 | 1,326 .0444947 .2062693 0 1
    24 | 1,326 .0422323 .2011945 0 1
    -------------+---------------------------------------------------------
    25 | 1,326 .0392157 .194181 0 1
    26 | 1,326 .0384615 .1923802 0 1
    27 | 1,326 .0369532 .1887182 0 1
    28 | 1,326 .0354449 .1849714 0 1
    29 | 1,326 .0286576 .1669053 0 1
    -------------+---------------------------------------------------------
    30 | 1,326 .0188537 .1360595 0 1
    31 | 1,326 .015083 .121929 0 1


    I've tried running the code in both a piecemeal fashion and in a single block as other users have suggested, but still no luck -- can't understand why 0.tteshift is "not found" when the variable does exist in my dataset. Any/all troubleshooting suggestions would be greatly appreciated!

  • #2
    You do not show us the regression, nor any example data, so we are left to speculate the cause of the problem. But what I suspect is happening is that although tteshift does take on the value 0 in your data set, that none of those observations are part of the regression. This can happen because the regression only uses observations which have non-missing values for every variable that is mentioned in the regression command. It may just be that when tteshift == 0 there is always a missing value in some regression variable. In that case, tteshift is never 0 in the regression sample, and so there is no _b[0.tteshift].

    You can see this illustrated in the following example:
    Code:
    sysuse auto, clear
    
    //  THIS WORKS FINE
    regress price mpg i.rep78
    levelsof rep78, local(reps)
    gen rep78_coef = .
    foreach r of local reps {
        replace rep78_coef = _b[`r'.rep78]
    }
    
    //  NOW REPLACE MPG BY MISSING VALUE WHENEVER REP78 == 1
    replace mpg = . if rep78 == 1
    
    //  AND THE SAME CODE NOW BREAKS WITH A "NOT FOUND" ERROR MESSAGE
    regress price mpg i.rep78
    levelsof rep78, local(reps)
    gen rep78_coef2 = .
    foreach r of local reps {
        replace rep78_coef2 = _b[`r'.rep78]
    }
    [1.rep78] not foundi
    r(111);
    So the first thing is to test my hypothesis in your data. I predict that if you run -levelsof tteshift if e(sample)- after the regression, you will not find 0 in it.

    If I am right, then there are two possibilities. The first is that there is something wrong with your data: there should be some observations with tteshift == 0 that have non-missing values for all the variables, but something has gone wrong with the data management to this point, so they aren't there. In that case you have to fix the data set. The other possibility is that there is nothing wrong with the data: it is a fact of life that whenever tteshift = 0 there is, appropriately, a missing value in some variable. In that case, you need to have the code skip over 0 (and any other values of tteshift that might have the same problem.) Continuing to build on the example shown above:

    Code:
    //  HOW TO GET AROUND IT:
    levelsof rep78 if e(sample), local(reps)
    gen rep78_coef3 = .
    foreach r of local reps {
        replace rep78_coef3 = _b[`r'.rep78]
    }
    If this is not what's going on in your situation, post back showing the actual regression command and example data (using the -dataex- command) that exhibits the same problem. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.


    Comment


    • #3
      Thank you, Clyde, the workaround did the trick! Turns out the tteshift variable did have some missing values that I missed. Appreciate the assistance, and will be sure to use -dataex- in the future.

      Comment

      Working...
      X