Announcement

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

  • Dealing with numbers from different rows

    Hello. I have a dataset like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(x1 x2) int x3 byte c
    1 1 121 .
    2 2 133 .
    3 3 132 .
    4 4 144 .
    end
    I need to give this instruction: in the row where x1 == 3, replace the value of c with the value of x3 when x2 == 2. The result must be like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(x1 x2) int(x3 c)
    1 1 121   .
    2 2 133   .
    3 3 132 133
    4 4 144   .
    end
    How can I do this?

    Thanks


  • #2
    perhaps,
    Code:
    replace c = x3[_n-1] if x1==3

    Comment


    • #3
      It doesn't work for me, because I need the result to depend on the value of x1 and x2, not the row number itself. Thank you anyway

      Comment


      • #4
        I see. How about,
        Code:
        sum x3 if x2==2
        replace c = r(mean) if x1==3
        ?

        Comment


        • #5
          It worked great! However, it strikes me that apparently this cannot be done through subscripting... thank you very much!

          Comment

          Working...
          X