Announcement

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

  • Help - Stata does not recognize an existing variable

    Hello friends,

    I am trying to use a loop in which a changing macro number variable refers to different variables. My variables are years, yr1971, yr 1972, yr 1973, and so forth. But Stata does not recognize the variables in the method I used (code below).

    Thank you.

    This is the relevant code segmant (followed by the stata output):

    count
    local obs = `r(N)'
    local qobs = `r(N)'/4
    foreach d of numlist 1(1)`qobs' {
    local x = (`obs'+4-(`d'*4))
    local begyr = 1971
    local endyr = 1978
    foreach f of varlist yr1971-yr2019 {
    local yrnum = 1
    foreach g of numlist `begyr'(`begyr')`endyr' {
    replace tempNR = yr`g'[`j'] in `yrnum'
    local yrnum = `yrnum'+1
    }
    local begyr = `begyr'+1
    local endyr = `endyr'+1
    }
    }


    . count
    464

    . local obs = `r(N)'

    . local qobs = `r(N)'/4

    . foreach d of numlist 1(1)`qobs' {
    2. local x = (`obs'+4-(`d'*4))
    3. local begyr = 1971
    4. local endyr = 1978
    5. foreach f of varlist yr1971-yr2019 {
    6. local yrnum = 1
    7. foreach g of numlist `begyr'(`begyr')`endyr' {
    8. replace tempNR = yr`g'[`j'] in `yrnum'
    9. local yrnum = `yrnum'+1
    10. }
    11. local begyr = `begyr'+1
    12. local endyr = `endyr'+1
    13. }
    14. }
    yr1971 not found
    r(111);

    end of do-file

    r(111);

    .

  • #2
    Without data it is hard to understand what is going on. Have you tried

    r(N) instead of `r(N)'?

    Comment


    • #3
      The scaler r(N) works fine. How exactly can I share the data? in line 8, yr`g' gives yr1971 ('g' is 1971 for the first execution of the loop). yr1971 in certainly an existing variable, but still it does not recognize it

      Comment


      • #4
        Nevermind, it now works, I have no idea why, Thanks!

        Comment

        Working...
        X