Announcement

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

  • by id: replace var1[1] = max(var1), error "weights not allowed"

    Dear community
    I have the following types of panel data:
    The actual data is huge, so ask questions to reduce the workload as much as possible.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id var1 target1 target2)
    1 . 4 4
    1 4 . 4
    1 4 . 4
    1 . . .
    2 . 8 8
    2 . . 8
    2 8 . 8
    2 . . .
    end
    I want to make target1 or target2 column as simple as possible using var1 column.
    It doesn't matter whether target1 or target2.

    The way I thought about it is as follows in the code
    Code:
    by id: replace var1[1] = max(var1)
    Then there's an error called "weights not allowed."

    thanks!

  • #2
    You need to better explain what you want to achieve.

    by id: replace var1[1] = max(var1)
    From this, I gather that you want to replace the first observation of var1 in a group with the maximum value within that group. If so, the following will do

    Code:
    gen order=_n
    bys id (order): replace order=_n
    bys id (var1): replace var1=var1[1] if order==1
    sort id order
    Res.:

    Code:
    . l, sepby(id)
    
         +---------------------------------------+
         | id   var1   target1   target2   order |
         |---------------------------------------|
      1. |  1      4         4         4       1 |
      2. |  1      4         .         4       2 |
      3. |  1      4         .         4       3 |
      4. |  1      .         .         .       4 |
         |---------------------------------------|
      5. |  2      8         8         8       1 |
      6. |  2      .         .         8       2 |
      7. |  2      8         .         8       3 |
      8. |  2      .         .         .       4 |
         +---------------------------------------+
    If values are not constant within a group

    Code:
    gen var1n=-var1
    bys id (var1n): replace var1=var1[1] if order==1
    sort id order
    as smaller values are sorted first, and the largest negative value is the smallest.
    Last edited by Andrew Musau; 15 Aug 2019, 03:25.

    Comment


    • #3
      Subscripts such as [1] are not allowed on the left-hand side of a replace statement. Stata is misreading them as weights.

      Looking ahead, your use of max() would fail too, as max() with replace requires two or more arguments. The help for once does not explain this well.

      Andrew Musau's code in fact gives the minimum, not the maximum.

      The simplest way to get a minimum or maximum for groups is arguably with egen,


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(id var1 target1 target2)
      1 . 4 4
      1 4 . 4
      1 4 . 4
      1 . . .
      2 . 8 8
      2 . . 8
      2 8 . 8
      2 . . .
      end
      
      bysort id : egen max = max(var1)
      bysort id : egen min = min(var1)
      bysort id (var1) : gen min2 = var1[1]
      
      list, sepby(id)
      
           +--------------------------------------------------+
           | id   var1   target1   target2   max   min   min2 |
           |--------------------------------------------------|
        1. |  1      4         .         4     4     4      4 |
        2. |  1      4         .         4     4     4      4 |
        3. |  1      .         4         4     4     4      4 |
        4. |  1      .         .         .     4     4      4 |
           |--------------------------------------------------|
        5. |  2      8         .         8     8     8      8 |
        6. |  2      .         .         8     8     8      8 |
        7. |  2      .         8         8     8     8      8 |
        8. |  2      .         .         .     8     8      8 |
           +--------------------------------------------------+
      If I say simplest, why do I say that as the code for min2 above shows that sorting gives a direct way to find minima? It's because the corresponding

      Code:
      bysort id (var1) : gen max2 = var1[_N]
      does not ignore missing values in the way that people usually want.
      Last edited by Nick Cox; 15 Aug 2019, 03:44.

      Comment


      • #4
        Andrew Musau's code in fact gives the minimum, not the maximum.
        That's correct Nick, here the maximum is the minimum as there is no variation within groups. I have added a condition in #2 to ensure that one obtains a maximum.

        Comment


        • #5
          Dear Andrew Musau & Nick Cox

          Thank you very much.
          So I knew what the problem was with my code.
          Also, I've learned many different ways!
          It was very helpful.
          Thank you.

          Comment

          Working...
          X