Announcement

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

  • Invalid syntax

    Invalid syntax

    Today, 07:03
    Hi. Hope you can help me with the following question:

    I am trying to run this code:
    regress ln_PERCAPITA ethnici1 edumo2 edumo3 edufa2 edufa3 birthreg2 birthreg3 [iw=FEX_C] if FG==1

    matrix b= e(b)'
    mat list b

    predict mu

    gen mu_tilde= exp(mu) if FG==1
    label var mu_tilde

    gen e= ln_PERCAPITA-mu if FG==1

    sum ethnici1 [iw=FEX_C] if FG==1
    local ethnici1= `r(mean)'
    sum edumo2 [iw=FEX_C] if FG==1
    local edumo2= `r(mean)'
    sum edumo3 [iw=FEX_C] if FG==1
    local edumo3= `r(mean)'
    sum edufa2 [iw=FEX_C] if FG==1
    local edufa2= `r(mean)'
    sum edufa3 [iw=FEX_C] if FG==1
    local edufa3= `r(mean)'
    sum birthreg2 [iw=FEX_C] if FG==1
    local birthreg2= `r(mean)'
    sum birthreg3 [iw=FEX_C] if FG==1
    local birthreg3= `r(mean)'


    mat C = (`ethnici1', `edumo2', `edumo3', `edufa2', `edufa3', `birthreg2', `birthreg3', 1)
    mat list C

    mat v= C*b
    mat list v
    gen v= `ethnici1'*`b1' +`edumo2'*`b2' +`edumo3'*`b3' + `edufa2'*`b4' + `edufa3'*`b5' +`birthreg2'*`b6' +`birthreg3'*`b7' + `b8'

    When I try to run the last line code of "gen v", stata generates r(198) error, and says invalyd syntax. What is wrong?

    That line is taking the created local variables (ethnici1, edumo2, edumo3...)and multiplying each of them with the estimated coefficients obtained in the regression and saved in the matrix b.


    Thanks in advance for any help!

  • #2
    I believe your problem is that you have written your code in the do-file editor window, and then rather than running everything at once, you are running it by selecting a few lines and running them, then selecting the next few lines and running them, and so on.

    Consider the following example. In the do-file editor window, I have a two-line program that I run in its entirety.
    Code:
    . do "/Users/lisowskiw/Downloads/example.do"
    
    . local message Hello, world.
    
    . display "The message is `message'"
    The message is Hello, world.
    
    . 
    end of do-file
    
    .
    Now I run the same two lines by selecting the first line and running it, then selecting the second line and running it.
    Code:
    . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD04017.000000"
    
    . local message Hello, world.
    
    . 
    end of do-file
    
    . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD04017.000000"
    
    . display "The message is `message'"
    The message is 
    
    . 
    end of do-file
    
    .
    The important thing to keep in mind is that local macros vanish when the do-file within which they were created ends. If you look carefully at the results above, you'll see that when I selected a single line to run, it was copied into a temporary do-file and run, so even though both lines are in the same window in the do-file editor, they are run as separate do-files, and local macro defined in the first line vanishes at the end of that do-file, and is undefined when the second line is run.

    So in your case, you write

    When I try to run the last line code of "gen v", stata generates r(198) error, and says invalyd syntax. What is wrong?
    and I think that means you have done what I described above, run your do-file a few lines at a time. When you run the generate command, none of the local macros are defined, so what Stata sees after substituting the local macros is
    Code:
    gen v= * +* +* + * + * +* +* +

    Comment


    • #3
      Where do you define b1 b2 and so on any way?

      Comment


      • #4
        Dear William and Nick, thanks for the replies.

        Nick, I forgot copying that line fo code after creating the b matrix...

        matrix b= e(b)'
        mat list b

        mat rowname b = b1 b2 b3 b4 b5 b6 b7 b8
        mat list b

        William, I am doing what you suggest, to run everything at a time...but I still get the error. I run it at the command window as well....and it's not working

        Comment


        • #5
          You are mistakenly assuming that the local macro `b1' will contain the value of the (only column in the) b1 row of matrix b. That is not the case.

          This should do what you want, without bothering to create all the local macros.
          Code:
          gen v= `ethnici1'*b[1,1] +`edumo2'*b[2,1] +`edumo3'*b[3,1] + `edufa2'*b[4,1] + `edufa3'*b[5,1] +`birthreg2'*b[6,1] +`birthreg3'*b[7,1] + b[8,1]
          Or
          Code:
          forvalues i=1/8 {
              local b`i' = b[`i',1]
          }
          if you have some reason to want the local macros.

          Comment


          • #6
            William!!!! Great!!!! It worked. I was indeed assuming what you mentioned :S.
            Thank you!!!!!

            Comment

            Working...
            X