Announcement

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

  • Inserting neighbor information from another row and variable

    Dear All,

    I would like to insert information from one's right neighbor to an individual i's row of observations. Let's imagine I have the following minimal example:
    HHID Age Right_Neighbor_HHID Neighbor_Age
    1 30 3
    2 40 1
    3 50 2
    Aiming to create a variable Neighbor_Age, I would like the get the age information from the respective right neighbor HHID, such as:
    HHID Age Right_Neighbor_HHID Neighbor_Age
    1 30 3 50
    2 40 1 30
    3 50 2 40
    The idea is to jump the row of one's neighbor and extract the age information from there. It seems super easy, yet, I am clueless how to code this in Stata. Of course, the peer information is not uniformly either below or above the row of interest.

    Does anyone have ideas?


  • #2
    For this example, the HHID is identified by the observation number, so

    Code:
    gen Neighbor_Age= Age[Right_Neighbor_HHID]
    will do it. But I suspect this does not hold generally, so a rename and merge is one possibility.

    Code:
    preserve
    keep HHID Age
    rename (HHID Age)  (Right_Neighbor_HHID Neighbor_Age)
    bys Rig Neigh: keep if _n==1
    tempfile merge
    save `merge'
    restore
    merge m:1 Right_Neighbor_HHID using `merge', nogen

    Comment


    • #3
      Here is another way to do it using rangestat (SSC)

      Code:
      clear
      input HHID    Age    Right_Neighbor_HHID    
      1    30    3    
      2    40    1    
      3    50    2
      end 
      
      rangestat Neighbor_Age=Age, int(HHID Right_Neighbor_HHID Right_Neighbor_HHID)
      
      list 
      
           +----------------------------------+
           | HHID   Age   Right_~D   Neighb~e |
           |----------------------------------|
        1. |    1    30          3         50 |
        2. |    2    40          1         30 |
        3. |    3    50          2         40 |
           +----------------------------------+

      Comment


      • #4
        Thank you so much - it's working perfectly. Missing values are replaced by sample average values but I will just replace them as missings afterwards.

        Comment

        Working...
        X