Announcement

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

  • Invalid name using forvalues

    Hello everybody,
    I am trying to save some lines of commands using loops but I am having some troubles in understanding which is the problem with these lines
    Code:
    forvalues i = 14/21 {
    egen avgpriceUSD_lead`i' = rowmean($gprice_USD_lead`i')
    }
    Stata displays an r(198) error, "14 invalid name". I have looked at forvalues help file but it seems to me that the syntax I have used is correct.

    Which can be the problem?
    Thank you

  • #2
    Stata looks for

    Code:
    $gprice_USD_lead
    as a global macro, doesn't find it, says "Oh well, I don't care" and substitutes an empty string. Then your first call becomes

    Code:
    egen avgpriceUSD_lead14 = rowmean(14)
    and Stata looks at the argument of rowmean() and says "no such variable exists, so invalid name".

    So, what you do think you are doing with

    Code:
    $gprice_USD_lead`i'
    ?

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Stata looks for

      Code:
      $gprice_USD_lead
      as a global macro, doesn't find it, says "Oh well, I don't care" and substitutes an empty string. Then your first call becomes

      Code:
      egen avgpriceUSD_lead14 = rowmean(14)
      and Stata looks at the argument of rowmean() and says "no such variable exists, so invalid name".

      So, what you do think you are doing with

      Code:
      $gprice_USD_lead`i'
      ?

      I should have written that I have defined global macros with that names, i.e. gprice_USD_lead14, gprice_USD_lead15, ... until lead21. What I am trying to do is to compute row means for this macros that contain a list of variables themselves. So I need to compute avgpriceUSD_lead14, avgpriceUSD_lead15, ... until lead21.

      Comment


      • #4
        OK, but you apparently did not define a global macro price_USD_lead. When Stata encounters $gprice_USD_lead`i', it first evaluates $grpice_USD_lead and comes up with nothing, so evaluates it to a null string. Then it looks at `i' and notes that it is 14. So the code asks for rowmean(14), as Nick pointed out.

        You need to force Stata to evaluate `i' as 14 and then evaluate $gprice_USD_lead14. That is done with the notation ${gprice_ USD_Lead`i'}.

        All of that said, the use of global macros is a dangerous coding practice and should be avoided unless you are in the rare situation where a local macro truly cannot do the job. If I were you I would get rid of all those global macros and create local macros with the same names and values instead. A small plus, besides the safety it provides, is that the expression `gprice_USD_lead`i'' poses no ambiguity about the order of evaluation of its components, so you would never have encountered the problem you have here.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          OK, but you apparently did not define a global macro price_USD_lead. When Stata encounters $gprice_USD_lead`i', it first evaluates $grpice_USD_lead and comes up with nothing, so evaluates it to a null string. Then it looks at `i' and notes that it is 14. So the code asks for rowmean(14), as Nick pointed out.

          You need to force Stata to evaluate `i' as 14 and then evaluate $gprice_USD_lead14. That is done with the notation ${gprice_ USD_Lead`i'}.

          All of that said, the use of global macros is a dangerous coding practice and should be avoided unless you are in the rare situation where a local macro truly cannot do the job. If I were you I would get rid of all those global macros and create local macros with the same names and values instead. A small plus, besides the safety it provides, is that the expression `gprice_USD_lead`i'' poses no ambiguity about the order of evaluation of its components, so you would never have encountered the problem you have here.

          I had defined a global macro using the usual command
          Code:
          global gprice_USD_lead14 gprice_USD1_lead14 gprice_USD2_lead14 gprice_USD3_lead14 gprice_USD4_lead14 gprice_USD5_lead14 gprice_USD6_lead14 gprice_USD7_lead14 gprice_USD8_lead14 gprice_USD9_lead14 gprice_USD10_lead14 gprice_USD11_lead14 gprice_USD12_lead14
          .

          However, I will follow your advice about using local macros instead of global. It seems more intuitive. Thanks.

          Comment


          • #6
            You don't even need to use a local macro, as that variable list should collapse to a wildcarded list such as

            Code:
            gprice_USD?_lead14 gprice_USD??_lead14
            which you can feed to rowmean() as such.

            Comment


            • #7
              Originally posted by Nick Cox View Post
              You don't even need to use a local macro, as that variable list should collapse to a wildcarded list such as

              Code:
              gprice_USD?_lead14 gprice_USD??_lead14
              which you can feed to rowmean() as such.

              Sure, I needed to understand the combination of local macros and forval but that's a good hint.

              Just another small question: now I have to compute the differences in average prices and they are build for example as delta_avgpriceUSD15 = avgpriceUSD_lead15 - avgpriceUSD_lead14. How can I write the code? This does not work:

              Code:
              forval i = 15/21 {
              gen delta_avgpriceUSD`i' = avgpriceUSD_lead`i' - avgpriceUSD_lead[`i'_-1]
              }
              Thanks

              Comment


              • #8
                That's confusing subscripting with name manipulation. You can do it on the fly, but this is how to start:

                Code:
                forval i = 15/21 {    
                    local j = `i' - 1      
                    gen delta_avgpriceUSD`i' = avgpriceUSD_lead`i' - avgpriceUSD_lead`j'
                }

                Comment

                Working...
                X