Announcement

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

  • invalid syntax

    Hello to the readers! I am estimating discretionary accruals by using a modified jones model. I am pretty sure I am using all the correct symbols. Nevertheless, the error r(198) comes up.

    This is the part where the mistake occurs:
    sum sic2id
    local k=r(max)
    forvalues i=1(1) `k'{
    qui reg tacc inverse_a drev ppe if sic2id== `i'
    qui predict res if sic2id== `i', res
    qui replace dac=res if sic2id== `i'
    qui replace r2a=e(r2_a) if sic2id==`i'
    qui replace b1=_b[inverse_a] if sic2id== `i'
    qui replace b2=_b[drev] if sic2id== `i'
    qui replace b3=_b[ppe] if sic2id== `i'
    qui drop res
    di  `i' " / " `k'
    }

    I have read multiple posts like mine and tried to solve it like you guys:
    What I tried to do is:


    gen Jones_Modified = .
    forval y = 2010(1) 2019 {
    forval i = 1(1) {
    display `i'
    display `y'
    capture reg tacc inverse_a drev ppe if `i' == sic2id & `y' == fyear, noconstant
    if c(rc) == 0 { // SUCCESSFUL REGRESSION
    predict r if `i' == sic2id & `y' == fyear, resid
    replace Jones_Modified = r if `i' == sic2id & `y' == fyear
    drop r
    }
    else if !inlist(c(rc), 2000, 2001) { // UNANTICIPATED ERROR
    display as error "Unexpected error encountered"
    exit c(rc)
    }
    else { // NO OR INSUFFICIENT OBSERVATIONS; NOTIFY & PROCEED
    display "No or insufficient observations"
    }
    }
    }

    But the mistake is the same. The ticks are accurate and I also tried to play with spaces.


    Could you please look at my code.
    Very much appreciated.

  • #2
    Code:
    forval i = 1(1) {
    looks wrong to me.

    Comment


    • #3
      Use "set trace on" and "set tracedepth 1" to get Stata to echo individual commands within the loop. Then search the log for the error message - Stata will continue to echo commands till the end of the loop, but there should be an error message just after the offending command. It would be nice if there was a consistent string to search for, but there isn't, so you have to make a visual search for the message. If Stata told you that there was "invalid syntax" somewhere within the loop, you can search for that string.

      Comment


      • #4
        The problem with the first set of code in post #1 is that in
        Code:
        forvalues i=1(1) `k'{
        the apparent space character following i=1(1) is in fact some sort of invisible character that is not a space, which confuses Stata. Perhaps this code was copied from some other topic in Statalist; problems like that happen occasionally because of issues with the Forum software.

        Removing the space
        Code:
        forvalues i=1(1)`k'{
        solves the syntax error problem, but points out a similar problem further on. In
        Code:
        di  `i' " / " `k'
        there are two spaces following di and one of them is again an invisible character that is not a space. Deleting both characters and then inserting a single space solves the problem.

        Comment


        • #5
          Thank you very much. It worked.

          Comment

          Working...
          X