Announcement

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

  • wrapping up using foreach v

    Hi
    using the below loop, I could nicely generate a maximum value of my variables of interest. I was wondering if there is any way to wrap up the rest of my codes in a loop.
    Thanks,
    Nader

    foreach v of varlist TLE_* HLE_* PLE_* {

    egen max_`v' = max(`v')

    }

    *50_51
    sum TLE_50_512 if TLE_50_512 > max_TLE_50_511
    sum TLE_50_513 if TLE_50_513 > max_TLE_50_511
    sum HLE_50_512 if HLE_50_512 > max_HLE_50_511
    sum HLE_50_513 if HLE_50_513 > max_HLE_50_511
    sum PLE_50_512 if PLE_50_512 > max_PLE_50_511
    sum PLE_50_513 if PLE_50_513 > max_PLE_50_511
    *60-61
    sum TLE_60_612 if TLE_60_612 > max_TLE_60_611
    sum TLE_60_613 if TLE_60_613 > max_TLE_60_611
    sum HLE_60_612 if HLE_60_612 > max_HLE_60_611
    sum HLE_60_613 if HLE_60_613 > max_HLE_60_611
    sum PLE_60_612 if PLE_60_612 > max_PLE_60_611
    sum PLE_60_613 if PLE_60_613 > max_PLE_60_611
    *70-71
    sum TLE_70_712 if TLE_70_712 > max_TLE_70_711
    sum TLE_70_713 if TLE_70_713 > max_TLE_70_711
    sum HLE_70_712 if HLE_70_712 > max_HLE_70_711
    sum HLE_70_713 if HLE_70_713 > max_HLE_70_711
    sum PLE_70_712 if PLE_70_712 > max_PLE_70_711
    sum PLE_70_713 if PLE_70_713 > max_PLE_70_711



  • #2
    I believe the following does it:
    Code:
    forvalues i = 5/7 {
        local thing `i'0_`i'1
        foreach p in TLE HLE PLE {
            forvalues j = 2/3 {
                summ `p'_`thing'`j' if `p'_`thing'`j' > max_`p'_`thing'1
            }
        }
    }
    As you can see, it's a bunch of nested loops. I don't think it can be done in a single loop.

    Note: As no sample data was provided, this code is not tested. Beware of typos and bugs.

    Comment

    Working...
    X