Announcement

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

  • _= invalid name r(198)

    Hi, I'm just getting back into Stata after a long illness, so I could use some help. I'm trying to run the following code to output proportions to an excel file. It runs through A`'j' but fails at C`j' and returns error code _=invalid name r(198). Can anyone suggest a fix?

    Here's my code:

    local j = 2
    foreach v of varlist agecat gender education incomecat race {
    svy,subpop(if child==1): prop `v',over(year)
    matrix results = r(table)
    matrix results = results[1...,1...]'
    putexcel set "$results\Table 1.xlsx", sheet("Categorical vars") modify
    putexcel A`j' = "`v'"
    local l = rowsof(results)
    forvalues `i' = 0(1)`l' {
    local i = `i' + 1
    local j = `j' + 1
    putexcel C`j' = results[`i',1] J`j' = results[`i',2]
    /*Above is the point of failure at C`j'*/

    local i = `i' + 1
    putexcel D`j' = results[`i',1] K`j' = results[`i',2]

    local i = `i' + 1
    putexcel E`j' = results[`i',1] L`j' = results[`i',2]

    local i = `i' + 1
    putexcel F`j' = results[`i',1] M`j' = results[`i',2]

    local i = `i' + 1
    putexcel G`j' = results[`i',1] N`j' = results[`i',2]

    local i = `i' + 1
    putexcel H`j' = results[`i',1] O`j' = results[`i',2]
    }
    local j = `j' + 2
    }


  • #2
    forvalues `i' = 0(1)`l' {
    You want

    Code:
    forvalues i = 0(1)`l' {
    Last edited by Andrew Musau; 10 Aug 2020, 13:34.

    Comment


    • #3
      I think your error(s) lie within these commands.
      Code:
      local l = rowsof(results)
      forvalues `i' = 0(1)`l' {
      local i = `i' + 1
      In particular
      Code:
      forvalues `i' = ...
      will substitute the value of the (undefined) local macro i for `i' effectively executing the command
      Code:
      forvalues  = ...
      with the following sort of result.
      Code:
      . clear all
      
      . local i
      
      . forvalues `i' = 1(1)2 {
        2. display "hello"
        3. }
      _= invalid name
      r(198);
      Syntactically, you wanted
      Code:
      forvalues i = ...
      With that said, you do not want the local macro i to be the index of your loop, and then to change its value within the loop, do you?

      Comment


      • #4
        Hi Andrew and William, thank you for your help with this. You were right about i vs. `i'. I made the change and was able to get the code to run. Here is the final code:

        local i = 0
        local j = 2
        foreach v of varlist agecat gender education incomecat race {
        svy,subpop(if child==1): prop `v',over(year)
        matrix results = r(table)
        matrix results = results[1...,1...]'
        putexcel set "$results\Table 1.xlsx", sheet("Categorical vars") modify
        putexcel A`j' = "`v'"
        local l = rowsof(results)
        forvalues i = 0(6)`l' {
        /*0(number of columns of the table)`total number of rows in matrix'*/

        local i = `i' + 1
        local j = `j' + 1
        putexcel C`j' = results[`i',1] J`j' = results[`i',2]

        local i = `i' + 1
        putexcel D`j' = results[`i',1] K`j' = results[`i',2]

        local i = `i' + 1
        putexcel E`j' = results[`i',1] L`j' = results[`i',2]

        local i = `i' + 1
        putexcel F`j' = results[`i',1] M`j' = results[`i',2]

        local i = `i' + 1
        putexcel G`j' = results[`i',1] N`j' = results[`i',2]

        local i = `i' + 1
        putexcel H`j' = results[`i',1] O`j' = results[`i',2]
        }
        }

        William, to answer your question, I think yes, I did want i to be the index for the forvalues loop. Again, still a little clunky from my illness, but this code produced the result I was looking for. Thanks again for your help.

        Comment

        Working...
        X