Announcement

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

  • Replace values of one variable according to the categories of another

    Hi all!

    I hope you can guide me in the following question. In te following dataset:

    id Ya Yb id_2
    1-1 .6249384 .7929984 1-1
    1-1 . . 1-2
    1-1 . . 2-1
    1-2 . . 1-1
    1-2 .6531338 .5850651 1-2
    1-2 . . 2-1
    2-1 . . 1-1
    2-1 . . 1-2
    2-1 .8717756 .890541 2-1

    How can I replace the value of Ya for id == 1,1, for all observations that meet id == 1,1?
    and so also for Yb, but with the variable id_2?

    The goal is to get the following dataset:

    id Ya Yb id_2
    1-1 .6249384 .7929984 1-1
    1-1 .6249384 .5850651 1-2
    1-1 .6249384 .890541 2-1
    1-2 .6531338 .7929984 1-1
    1-2 .6531338 .5850651 1-2
    1-2 .6531338 .890541 2-1
    2-1 .8717756 .7929984 1-1
    2-1 .8717756 .5850651 1-2
    2-1 .8717756 .890541 2-1

    Thank you very much in advance.

    Greetings,

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3 id float(Ya Yb) str3 id_2
    "1-1" .6249384 .7929984 "1-1"
    "1-1"        .        . "1-2"
    "1-1"        .        . "2-1"
    "1-2"        .        . "1-1"
    "1-2" .6531338 .5850651 "1-2"
    "1-2"        .        . "2-1"
    "2-1"        .        . "1-1"
    "2-1"        .        . "1-2"
    "2-1" .8717756  .890541 "2-1"
    end
    
    bys id (Ya): replace Ya = Ya[_n-1] if mi(Ya)
    bys id_2 (Yb): replace Yb = Yb[_n-1] if mi(Yb)

    Comment


    • #3
      Assuming "Ya" and "Yb" are numeric variables:

      Code:
      bys id (Ya): replace Ya= Ya[1]
      bys id_2 (Yb): replace Yb= Yb[1]

      Comment

      Working...
      X