Announcement

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

  • Looping over two variables at once

    Hi !
    I have been using STATA not for so long. And now I'm trying to loop over 2 variables at the same time in order to generate a new variable. My data, as shown in the attached dta file looks like lpermno trt1m (which is the monthly stock returns) year mydatemonthly rf.
    The question is that, for every lpermno, I want to generate monthly excess returns=trt1m/100-rf.
    I tried doing the following but it's not working:

    egen group = group( lpermno mydatemonthly)
    su group, meanonly
    foreach i of num 1/`r(max)' {
    gen ret= trt1m- rf if group == `i'
    } //invalid numlist has too many elements

    Then, I tried this but in vain:
    egen group1 = group(mydatemonthly)
    su group1, meanonly
    local maxrf = r(max)

    egen group2 = group( lpermno)
    su group2, meanonly
    local maxlpermno = r(max)

    foreach i of num 1/`maxlpermno' {
    foreach t of num 1/`maxrf' {
    gen ret= trt1m/100- rf if group1 == `i' & group2 == `t'
    }
    }

    Any suggestions are welcome !

  • #2
    Have you tried -by:-. For example:

    Code:
    bysort group: gen ret = trt1m - rf
    See -help by-.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      Also, try formatting your code within posts with code tags. Use the "advanced editor" button. That's something like A in the top-right corner of your editor. Code is much easier to read that way.
      You should:

      1. Read the FAQ carefully.

      2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

      3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

      4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

      Comment


      • #4
        Duplicated in this forum:

        http://www.statalist.org/forums/foru...iables-at-once
        You should:

        1. Read the FAQ carefully.

        2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

        3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

        4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

        Comment


        • #5
          ... and -by- doesn't make sense here. Sorry.
          You should:

          1. Read the FAQ carefully.

          2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

          3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

          4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

          Comment

          Working...
          X