Announcement

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

  • if command in loop procedure.

    I'm trying to use if command in loop procedure. But I got the message " 'if' found where almost anything else expected" whenever I use if- command .

    The code that I wrote is presented below.



    kmat=J(10,3,1)
    N=rows(kmat)

    for (i=1; i<=N;i++) {
    tmp1=0
    for (j=1; j<=N; j+++ {
    if ([j]!=[i]) {
    tmp1 = tmp1 + (2*Kmat[i,j])
    }
    }
    }

    And I got the error message('if' found where almost anything else expected") when if command is presented.

    What's the wrong with this code?




  • #2
    ha lee parenthesis missing at the end of for (j=1..., this is what triggers the error message...and it should be (for example)

    Code:
    for (j = 1 ; j<= cols(kmat); j++) // kmat has only 3 columns not N
    you don't need the brackets in the if condition, so it should be
    Code:
    if (i!=j)
    and
    Code:
    tmp1 = tmp1 + (2*kmat[i,j]) // not Kmat
    Last edited by Christophe Kolodziejczyk; 17 Jul 2017, 02:35.

    Comment


    • #3
      Hi, good evening.

      I have a sample of 10 industries and 20 years and I need to run the regression in looping by industry (for one model) and by year (for another model) and create a new variable with the residuals (for one model) and the coefficient (for another model) of the regression.

      Can anyone help me with that?

      Thanks

      Ana Siqueira

      Comment


      • #4
        If what you want is the residuals, just run that model with reghdfe 4.x:


        Code:
        sysuse auto, clear
        
        reghdfe price, absorb(foreign##c.(gear length)) residuals(resid1)
        
        * Check that it works
        reg price gear length if foreign==0
        predict double resid2a, resid
        reg price gear length if foreign==1
        predict double resid2b, resid
        clonevar resid2 = resid2a
        replace resid2 = resid2b if foreign==1
        
        su resid1 resid2
        corr resid1 resid2
        Edit: TBH you don't particularly need reghdfe, you can run this with any regress command:

        Code:
        reg price foreign##c.(gear length)
        Last edited by Sergio Correia; 17 Jul 2017, 23:34. Reason: edit

        Comment


        • #5
          Ha Lee, You're missing a ")" in your second loop.

          Best,
          Best,
          Pablo Bonilla

          Comment

          Working...
          X