Announcement

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

  • Invalid Syntax

    We wanted to create an do-file for a coefficient-estimation for the previos 36 months for each fund and then calculate a 12-month ahead estimate for alpha , but when we want to apply this do file to our dataset we always get the error message 'invalid syntax' r(198), but stata does not specify where our problem is.


    //load dataset




    gen SPret = benchmark - rf


    //generate variables for results with momentum

    gen alpha = .


    gen beta_SPret = .

    gen beta_smb = .

    gen beta_hml = .

    gen beta_mom = .

    gen r2 = .

    //generate variables for results (without momentum)

    gen alpha_wom = .

    gen beta_SPret_wom = .

    gen beta_smb_wom = .

    gen beta_hml_wom = .

    gen r2_wom = .

    //begin loop



    //create group identifier with variable code

    egen id = group(name)

    sort name year month

    sum id

    local max `r(max)'


    forvalues i = 1/`max' {


    foreach id == `i' {

    local y = min(calmt) //create local min of calmt to know where to start
    local z = max(calmt) //create local max to identify ending point




    di in red "`c(current_time)': @id: `i'" //show loop results


    sum id if id == `i' // Calculate the sum of observations for each funds


    if `r(N)' > 47 { // There have to be at least 47 months calmt observations, because the coefficients are calculated over a period of 36 months and then an alpha is calculated 12 months ahead



    rolling _b, start(y) window(36) end(z-12) saving(betas_m) stepsize(1) : reg mgret SPret smb hml mom



    replace alpha = _b[_cons] if id == `i'


    replace beta_SPret = _b[SPret] if id == `i'


    replace beta_smb = _b[smb] if id == `i'


    replace beta_hml = _b[hml] if id == `i'


    replace beta_mom = _b[mom] if id == `i'


    replace r2 = e(r2) if id == `i'
    }
    }
    }

    di in red "`c(current_time)': @id: `i'" //show loop results

    sort name year month

    sum id

    local max `r(max)'


    forvalues i = 1/`max' {

    foreach id == `i' {


    local y = min(calmt) //create local min of calmt to know where to start
    local z = max(calmt) //create local max to identify ending point

    di in red "`c(current_time)': @id: `i'" //show loop results

    if `r(N)' > 47 {
    //regression without momentum
    rolling _b, start(y) window(36) end(z-12) saving(betas_wom) stepsize(1): reg mgret SPret smb hml




    replace alpha = _b[_cons] if id == `i'


    replace beta_SPret = _b[SPret] if id == `i'



    replace beta_smb = _b[smb] if id == `i'



    replace beta_hml = _b[hml] if id == `i'



    replace r2_wom = e(r2_wom) if id == `i'

    }
    }


    }


    }

  • #2
    This is a very old-fashioned piece of code. The forum contains many posts on community-contributed commands that will do much for this for you.

    That aside, you should be able to tell us more precisely where your code fails. To my eye, this looks OK

    Code:
    //load dataset
    gen SPret = benchmark - rf
    
    //generate variables for results with momentum
    gen alpha = .
    gen beta_SPret = .
    gen beta_smb = .
    gen beta_hml = .
    gen beta_mom = .
    gen r2 = .
    
    //generate variables for results (without momentum)
    gen alpha_wom = .
    gen beta_SPret_wom = .
    gen beta_smb_wom = .
    gen beta_hml_wom = .
    gen r2_wom = .
    
    //begin loop
    
    //create group identifier with variable code
    
    egen id = group(name)
    
    sort name year month
    
    sum id
    
    local max `r(max)'
    
    forvalues i = 1/`max' {
    But the next three statements would all fail.

    Code:
    foreach id == `i' {
    That is a long way from any legal variant on foreach. But you are already in a loop over your distinct identifiers. So, no inner loop is needed here any way.

    Code:
    local y = min(calmt) //create local min of calmt to know where to start
    local z = max(calmt) //create local max to identify ending point
    Those are illegal too. min() and max() as functions won't work on one argument. Nor do they know to operate differently within a loop according to the current loop value. Perhaps you meant to do something more like this:

    Code:
    su calmt if id == `i', meanonly
    local min = r(min)
    local max = r(max)
    Also, looking ahead

    Code:
    replace r2_wom = e(r2_wom) if id == `i'
    That's not going to work as you wish. regress doesn't leave e(r2_wom) in its wake.

    I'll stop there. Someone is mixing borrowed and guessed code. Borrowing often works, but guessing rarely.

    More generally, "mark zuckerberg" just possibly could be your real name, but we do flag that we request full real names on Statalist.

    Comment

    Working...
    X