Announcement

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

  • variable (that exists) "not found" in loop - says Stata

    sorry for the weird thread title.

    So here's what I write. It is a nested loop to find out whether some change in a variable bla corresponds approximately to a change in the variable bla for another observation within the same group:


    forvalues group = 1/2 {

    su position if group== `group' , meanonly

    forvalues from = 1/`r(max)' {
    forvalues to = 1/`r(max)' {

    bysort group: replace from_to = "`from'+"to"+`to'" if d_bla[`_from'] < upperbound[`_to'] & d_bla[`_from'] > lowerbound[`_to'] & group==`group'

    }
    }

    }



    and I get the following error:


    d_bla not found
    r(111);




    however, the variable exists. can anybody tell me what the error is?
    I know this will not be the most efficient way of programming, it's just the first solution I came up with...
    thank you very much!
    Last edited by Anya Fedyk; 12 Dec 2014, 08:19.

  • #2
    show us the result of "d d_bla"

    also, why are you putting underscores before "from" and "to" in your bysort statement? they don't belong

    Comment


    • #3
      On this evidence you are referring to local macros _from and _to that you have not defined. However, what Stata is complaining about is that it doesn't know what d_bla is. Rich is right that if it were a variable describe would show it, but I doubt you would get that error message if it were a variable. Similar, but not identical, comments if it were a scalar.

      In short, we can't advise here easily without knowing what everything mentioned in this code is supposed to be.

      Comment


      • #4
        thank you for your answers. I appreciate your help, unfortunately I could not access this until now.

        1) desc yields:

        storage display value
        variable name type format label variable label
        -------------------------------------------------------------------------------------
        d_bla float %9.0g


        2) I put underscores because I wanted to refer to observation `from' or `to' and thought I could achieve this with underscores, yielding results as in "_n". Is this not the case?


        /update: it works now without the underscores. It just takes VERY much time from what I can estimate. I will get back to you if it does not do what I wanted it to do. Thank you again!
        Last edited by Anya Fedyk; 15 Dec 2014, 03:13.

        Comment


        • #5
          You were clearly right that d_bla exists as a variable. As you haven't defined local macros _from or _to (and as they do not have in-built roles) your code will look to Stata like

          Code:
          forvalues group = 1/2 {
            su position if group== `group' , meanonly
            forvalues from = 1/`r(max)' {
              forvalues to = 1/`r(max)' {
                bysort group: replace from_to = "`from'+"to"+`to'" if d_bla[] < upperbound[] & d_bla[] lowerbound[] & group==`group'
              }
            }
          }
          My guess about the error message you got was wrong. But my next guess is that Stata was misled by the [] to thinking that you were trying to access a matrix with that name.

          Regardless of that, what you are trying to do with this code is quite unclear to me. I'd back up and explain what you want, with example data and example results.
          Last edited by Nick Cox; 15 Dec 2014, 03:51.

          Comment

          Working...
          X