Announcement

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

  • foreach issues

    Hi everyone! I have some issues in running two loops.
    What I want to do is to estimate alpha and beta coefficients for each id and date in my dataset. what I want to obtain is a different alpha and beta and residuals for each line.
    What I implemented is the following code:

    Code:
    g alpha=. 
    g beta1=.
    g residuals=.
    ​​​​​​​
    qui levelsof(date), local(date)
    foreach date of local date {
    
        levelsof id, local(levels) 
        foreach v of local levels  {
        
        gen date_250 = date-250 if date == `date'
        gen date_50 = date-50  if date == `date'
        egen d250 = mean(date_250)
        egen d50 = mean(date_50)
        format %tdDD/NN/CCYY date_250
        format %tdDD/NN/CCYY date_50
        format %tdDD/NN/CCYY d250
        format %tdDD/NN/CCYY d50
        gen check = (date>=d250 & date<=d50)
    
        quietly reg lnstock_return lnmkt_return if id ==`v' & check ==1, r
        predict temp2 if id== `v', resid 
        replace residuals = temp2 if id == `v' & date == `date'
        drop temp2
        replace alpha = _b[_cons] if id ==`v' & date == `date' 
        replace beta1 = _b[lnmkt_return] if id ==`v' & date == `date'
        }
    
    drop check d50 d250 date_*
    }
    However, my loops are not running correctly and I have an estimate only for one alpha and one beta, without any error message.

    Do you have any idea of why?

    Thanks for your help,

    Andrea



  • #2
    You should definitely see an error message which should tell you that you cannot generate a variable more than once. You need to move the line
    Code:
    drop check d50 d250 date_*
    to the end of
    Code:
    foreach v of local levels  {
    ...
    }
    or you move the lines
    Code:
       gen date_250 = date-250 if date == `date'
        gen date_50 = date-50  if date == `date'
        egen d250 = mean(date_250)
        egen d50 = mean(date_50)
        format %tdDD/NN/CCYY date_250
        format %tdDD/NN/CCYY date_50
        format %tdDD/NN/CCYY d250
        format %tdDD/NN/CCYY d50
        gen check = (date&gt;=d250 &amp; date&lt;=d50)
    into the first loop, for example before the line
    Code:
    levelsof id, local(levels)
    .

    Comment


    • #3
      Thank you very much! I followed your advice but aplha, beta1 and residuals variables are still missing after the loop ended.

      Comment


      • #4
        Hi Andrea,

        I think you should name your local variables in different way. For example, your codes:
        Code:
        qui levelsof(date), local(date)
        foreach date of local date {
            levelsof id, local(levels)
            foreach v of local levels  {
                 gen date_250 = date-250 if date == `date'
                 gen date_50   = date-50   if date == `date'
        I dont know which `date' Stata will pickup in line
        gen date_250 = date-250 if date == `date'
        . The `date' in levelof or in foreach?

        you could try "set trace on" command before your loop to see how it work and locate the problem!
        Last edited by cu dao huy; 10 Jun 2020, 12:35.

        Comment


        • #5
          Stata will set the macro to empty if it sees a construct like
          Code:
          foreach date of local date {
          This should explain why the code does not work. Unfortunately, Stata does not complain if it sees such a construct and will happily set the macro to empty.

          Comment


          • #6
            Originally posted by Sven-Kristjan Bormann View Post
            Stata will set the macro to empty if it sees a construct like
            Code:
            foreach date of local date {
            This should explain why the code does not work. Unfortunately, Stata does not complain if it sees such a construct and will happily set the macro to empty.
            Hi, pls put it in the wishlist for Stata 17 :>

            Comment


            • #7
              Hi, pls put it in the wishlist for Stata 17 :>
              I have only Stata 14. Do know you if the same behavior is observed also in Stata 16? Before I put it on the wishlist for Stata 17, I want to be sure that this behavior has not been fixed yet.
              Last edited by Sven-Kristjan Bormann; 11 Jun 2020, 13:24.

              Comment


              • #8
                Originally posted by Sven-Kristjan Bormann View Post
                I have only Stata 14. Do know you if the same behavior is observed also in Stata 16? Before I put it on the wishlist for Stata 17, I want to be sure that this behavior has not been fixed yet.
                Hi, I am using Stata version 15.1 SE window (64-bit x86-64). And trying:
                Code:
                local date 12345 12345 12345
                foreach date of local date {
                    di `date'
                }
                It's still happen on Stata 15.1 version! I think Stata 16 is still as well!

                Comment


                • #9
                  Ok. I have added it to the wishlist.

                  Comment

                  Working...
                  X