Announcement

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

  • egen with a varying number of variables

    I have a n variables x1 .. xn. Now I want to know for each of these n variables what the maximum value is of the n-1 other values.

    So suppose there are 4 variables, then I want to know for x1 the maximum value of x2, x3 and x4. And for x2, I want to know the maximum value of x1, x3 and x4. And so on.

    The variables can take any value, both negative and positive.

    Code:
    egen rmax = rowmax(x1 x2 x3)
    seems not to be possible because n can vary.

    Does anyone know how to handle this?

  • #2
    If the names of the variables follow a pattern, you can use wildcards. For example, if these are all of the variables that begin with x, and only those, you can write -egen rmax = rowmax(x*)-. See -help varlist- for more about wildcards.

    If you have no pattern in the variable names that can be expressed with wildcards, then there is nothing short of listing out all the variables by name. If what you have a loop that does some things with a different set of variables each time through, then there is probably some local macro available in the loop that contains the list and you can refer to that list as the argument of -rowmax()-. For example:

    Code:
    sysuse auto, clear
    local numerics rep78 headroom gear_ratio
    
    local mylist
    foreach v of varlist `numerics' {
        local mylist `mylist' `v'
        display `"`mylist'"'
        egen max_through_`v' = rowmax(`mylist')
    }
    list max_through* in 1/5
    If these ideas don't help you, I suggest you post back with more details about your specific situation.

    Comment


    • #3
      Suppose, as in #1, you have x1 x2 x3 x4. Then there is one variable for each which is the maximum of the others.

      Code:
      local all x1 x2 x3 x4
      
      foreach v of local all {
          local others : list all - v
          egen all_but_`v' = rowmax(`others')
      }

      Comment


      • #4
        The code in #3 comes close, however, as v can vary, the variables that should be added to the local all should also vary.
        So, if v is 3, I need
        Code:
        local all x1 x2 x3
        but if v is 5, I need
        Code:
        local all x1 x2 x3 x4 x5
        So how can I do that?

        Comment


        • #5
          I believe that my code solves the problem in #1

          I have a n variables x1 .. xn. Now I want to know for each of these n variables what the maximum value is of the n-1 other values.

          So suppose there are 4 variables, then I want to know for x1 the maximum value of x2, x3 and x4. And for x2, I want to know the maximum value of x1, x3 and x4. And so on.
          Apparently your problem is not what you said, but now I don't understand it.

          Comment


          • #6
            I call this code in a loop where n varies. The variables are named x`n'
            Depending from the number of n, I have multiple variables x, which are called x1, x2, x3 ... etc.
            So I cannot say
            Code:
            local all x1 x2 x3 x4
            as with an n of 6, I should have the following code
            Code:
            local all x1 x2 x3 x4 x5 x6

            Comment


            • #7
              I doubt that I can add more as I am still lost here.

              You seem to be saying that we misunderstood your problem. it seems that Clyde gets no marks at all and I get partial credit. OK, but if so, then you need to explain it so that someone does understand it.

              Also, I think you're saying that you have written a loop that doesn't do what you want -- but you don't even show your code.

              Comment


              • #8
                Ah, no, it has nothing to do with giving credits or not. I really appreciate the help of both you and Clyde! It's more that I didn't understand something in Nick's code, which is why I asked a question about it. In any case, I understand that I'm asking an unclear question, so first I'm going to think about how I can present it clearly.

                Comment

                Working...
                X