Announcement

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

  • Loop function

    Hello,

    I want to generate following:
    gen capm1 = rmrf if decile == 1
    and this 10x times, basically until
    gen capm10 = rmrf if decile == 10

    Do I have to type this in 10x myself or can I use foreach? Is foreach possible with function gen, because the variablename has to change every time?

    At the end I would also like to form 10 regressions looking like this, which is perhaps more possible with foreach? :
    reg decile1 capm1 until reg decile10 capm10

  • #2
    Code:
    forval i=1/10 {
    g capm`i'  =  rmrf if decile == `i'
    }
    
    //Regression:
    
    forval i=1/10 {
    
    reg decile`i'  capm`i'
    
    }
    Roman

    Comment


    • #3
      No loop command (not function) is needed here to generate new variables.

      Code:
      separate rmrf, by(decile) gen(capm)
      In a previous thread I wondered whether you really need separate variables, but that is a different question.

      Roman Mostazir's,second loop could equally be a loop over 10 if qualifiers. -- that is, using the technique in his first loop.

      Comment

      Working...
      X