Announcement

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

  • Help, error in using a local with a multiplicative function expression

    Hello to all statalist users,


    I'm doing a loop to get kdensity, but I have a problem. In the loop that I attached below, I do not run a line of code, which is very important and I do not know how to fix it.

    Where it fails is in the following line of code: local h1 = (`h1 ') * (` scaledown')

    The complete code is the following:

    Code:
    gen area2=zona_conasami2012=="Zona C"
    
    sum lrsalary
      global B=200
      gen r0=(_n-1)/($B-1)
      replace r0=. if _n>$B
    *r0 should span [0,1]
      global a=4
      global b=7
      replace r0=$a+r0*($b-$a)
    *r0 should span [$a,$b]
    
    *IMPORTANT: bandwidth "scaledown" factor for density estimation
    local scaledown=2
    
    forval yr=2012/2018 {
     sum lrsalary if area2==1 & year==`yr' [aw=weight_ta6], det
    di min(r(sd),((r(p75)-r(p25))/1.349))*(r(N)^(-1/5))*0.9
    local h1=min(r(sd),((r(p75)-r(p25))/1.349))*(r(N)^(-1/5))*0.9
    di `h1'
    local h1=(`h1')*(`scaledown')
    di `h1'
     kdensity lrsalary if area2==1 & year==`yr' [aw=weight_ta6], at(r0) gen(lrsalary_hat1_`yr') bwidth(`h1') nograph
     
     sum lrsalary if area2==0  & year==`yr' [aw=weight_ta6], det
    di min(r(sd),((r(p75)-r(p25))/1.349))*(r(N)^(-1/5))*0.9
    local h0=min(r(sd),((r(p75)-r(p25))/1.349))*(r(N)^(-1/5))*0.9
    di `h0'
    local h0=(`h0')*(`scaledown')
    di `h0'
     kdensity lrsalary if area2==0  & year==`yr' [aw=weight_ta6], at(r0) gen(lrsalary_hat0_`yr') bwidth(`h0') nograph
    }
    .
    Error in:

    Code:
    local h1=(`h1')*(`scaledown')
    invalid syntax
    r(198); t=0.00 19:01:05
    Thank you very much for the suggestions.

    Alexis Rodas

  • #2
    There is nothing obviously wrong with that command. And both local macros h1 and scaledown are defined earlier in the code.

    My best guess is that you are selecting blocks of lines of code to run separately, and, in particular, that the line in which local macro h1 or the line in which local macro scaledown (or both) is not included in the same block where you run the command that is throwing off this error message.

    When you select blocks of lines of code to run, they constitute a single program for the purpose of defining the scope of local macros. That is, once that block finishes running, any local macros defined therein cease to be defined. So if you define a local in one block of code but then try to use it in a separate block of code, it is undefined at that time. That would cause exactly the kind of error you are seeing here.

    Whenever you run blocks of code that refer to macros, the block must go all the way back to the place where that macro is defined. (If that's a very long block of code and if most of what's in it is irrelevant at the moment, you can comment out the intervening lines.)

    tldr; if you just run what you show in #1 all at one time, I predict this error message will vanish.

    Comment

    Working...
    X