Announcement

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

  • stata cannot find local variables

    Dear Stata users,

    I'd like your advice because I have been stuck. I have a dataset and I run some models, keeping the coefficients as a new dataset of 10rows and 20variables/ columns. All good till this point. In the new dataset (that of the coefficients) all variables are float and will be used to generate overlaid two-way graphs. I'm using the code that you can see below and the output is an error that the local variable (let's say main_2 cannot be found. Any idea what I should do...

    [foreach s in 2 3 4 5 7 8 9 {
    local s `s'
    local main_`s'= age_`s'c1
    local mainlb_`s'=age_`s'c3
    local mainub_`s'=age_`s'c4
    local ci_`s'
    }][/CODE]

  • #2
    Your question is unclear. There is no such thing in Stata as a local variable. There are variables, and there are local macros. The distinction between a macro and variable is important. A macro is a scalar: it is a single value (and can be either string or numeric). A variable is actually a vector: it is an array of values, one value per observation in the data set.

    You do not explain what age_*c* refer to. Are these (supposed to be) variables in your data set? If so, you should be awware that a statement like -local main_`s' = age_`s'c1- creates a local macro, main_`s', whose content is the value of the variable age_`s'c1 in the first observation. The values of age_`s'c1 in all other observations are not included. You should also be aware that main_`s' is, itself, not a variable and it cannot be used as if it were. If you try to use it in a command in a place where Stata expects to find a variable, you will get an error message saying that no such variable can be found. That apparently is what is happening to you (although I do not see any way you could get that message within the code you show in #1.) If you want to use the value that has been stored in main_2 (to be specific), which is the same as the value of age_2c1 in the first observation of the data set, then you have to refer to it as `main_2'. You cannot omit those macro quotes when you want to access the contents of a macro. So:

    Code:
    gen some_new_variable = age5c3 + main_2 // WRONG
    gen some_new_variable = age5c3 + `main_2' // CORRECT
    // BUT IT DOES NOT ADD TWO VARIABLES
    // IT ADDS THE CONSTANT VALUE OF age2c3[1] TO
    // VARIABLE age5c3 IN EVERY OBSERVATION
    If this explanation does not help you, I suggest you repost. When you do, instead of showing just code, show the code and also show all the output (including error messages) it generates. Don't show code without output, nor refer to error messages without the code that actually threw them--it isn't helpful.

    Comment


    • #3
      More than helpful! Thank you very much. I wanted to define the variable and not a value as local, so "=" is not correct. Many thanks, solved.

      Comment

      Working...
      X