Announcement

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

  • Dollar sign when defining local varaible

    Dear all,

    I'm trying to run some .do file, and receive an error on a line:

    Code:
    local carry=$carry_months
    Error:
    invalid syntax
    r(198);

    As far I googled, a dollar sign might mean global variable or, in some cases, just a dollar sign to be inside a variable? Could you please explain what this line might do and how can I avoid the error?

    After defining it, there is a loop:
    Code:
    local names="AS"
    
    foreach k in `names' {
        forval i=1/`carry' {
            sort stock month_id
        
            qui gen `k'_pr_`i'=`k'[_n-`i'] if stock==stock[_n-`i'] & month_id==month_id[_n-`i']+`i'
        }
    }
    
    foreach k in `names' {
        forval i=1/`carry' {
            sort stock month_id
        
            qui replace `k'=`k'_pr_`i' if `k'==. & stock==stock[_n-`i'] & month_id==month_id[_n-`i']+`i' 
            qui drop `k'_pr_`i' 
        }
    }
    Thank you in advance.

  • #2
    That's legal but surprising syntax. The dollar sign there certainly implies a global macro (not global variable).

    If the global foo contained say a variable name then

    Code:
    local bar = $foo
    would return the value of that variable in the first observation and it seems unlikely that is wanted.

    But, backing up, the most likely reason I can think of for your error message is that the global is not even defined. You can check for that directly by inserting previously

    Code:
    di "carry_months is $carry_months"
    and if nothing is displayed then that explains the error message.

    But without any context -- and without knowing what the global is supposed to contain -- it is really hard to suggest what the code should be.

    Conversely, this do-file presumably did what the author(s) wanted at some point, so why not ask them to explain?

    (Undocumented do-files can be much more work than undocumented programs!

    (As far as I can see your longer code segment casts no light on the error.)

    Comment


    • #3
      In
      Code:
      local carry=$carry_months
      Stata interprets $carry_months to be a global macro (not variable) named carry_months. If there is no such global macro defined, then $carry_months is evaluated as an empty string, so that your command reduces to:
      Code:
      local carry =
      which is, indeed, a syntax error because you can't have an equals sign with nothing to its right in Stata.

      If $carry_months is defined, then whatever it contains is substituted for $carry_months on the right side of that equals sign. If that doesn't happen to be a valid expression (only valid expressions can occur to the right of that equals sign) then this will also be a syntax error.

      To find out which of these situations applies to you, run -display `"$carry_months"'-.

      Edit: Crossed with Nick's response.

      Comment


      • #4
        Dear Nick and Clyde,

        you're right, the error is caused by the fact that variable $carry_months does not exist..
        Documenting is a nice thing in programming, because I can hardly have an idea of what might be implied.
        Thanks for your help.

        Comment

        Working...
        X