Announcement

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

  • Command rangestat cannot use local macros for interval

    such syntac report that: "was expecting a numeric variable, a number, or a system missing value for the interval high: "
    Code:
    forvalues i = 1 2  {
        rangestat (mean) Opnprc ChangeRatio, interval(StkSerial 0 `i') by(Stkid)
        renvarlab *_mean,postfix(Work`=`i'+1')
    }

  • #2
    I don't know how you even get to your -rangestat- command here. -forvalues i = 1 2- is a syntax error in its own right and ends execution there.

    So, I'm guessing that you are executing this code line by line, which does not work properly with code that involves local macros. Here's what's going on.

    Line 1: -forvalues i = 1 2 {- This is a syntax error. Stata gives you an invalid syntax message which you ignored.
    Line 2: -rangestsat ..., interval(StkSerial 0 `i') by(Stkid)- Because you are running line by line, local macro i is undefined, which means that Stata interprets references to it (`i') as an empty string (""). Note well that local macro i would still be undefined even had there been no syntax error in line 1, because local macros go out of existence when you attempt to run the code line by line.

    So, two lessons. Lesson 1. Never ignore a Stata error message. Always fix the problem before attempting to proceed. Lesson 2. Do not run code involving local macros line by line. Any code that includes local macros must be run without interruption from the first definition of the local macro to its last use. If you interrupt execution, the local macro goes out of existence.

    So fix the first line in your code to -forvalues i = 1/2 {-, and run the entire thing in one fell swoop, and you should be fine.

    Comment


    • #3
      When I get errors in a loop (especially complex ones), I comment everything out and just have a di `i' (or whatever) to make the sure the loop is working properly.

      You'll get the error here:

      Code:
      forv i = 1 2 {
          di `i'
      }
      But not here

      Code:
      forv i = 1/2 {
          di `i'
      }

      Comment


      • #4
        I see! My mistake!

        Comment


        • #5
          My guess is that you are not letting rangestat see the local macro. Google

          local macros have local scope Stata Journal

          for a paper with discussion.

          Comment

          Working...
          X