Announcement

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

  • Replacing several variable values with other several variable values

    Hi,

    I am trying to replace several variable values with other several variable values for one specific observation only (e.g. Person X).
    Data set looks similar to this (but with more variables)
    person aggall_var1 aggall_var2 aggall_var3 aggreg_var1 aggreg_var2 aggreg_var3
    X 1 2 3 4 5 6
    It would be great if it would look like this:
    person aggall_var1 aggall_var2 aggall_var3 aggreg_var1 aggreg_var2 aggreg_var3
    X 1 2 3 1 2 3
    As in the data example shown here, the ending of the variables that should be matched/replaced are the same.

    So in simple terms, the code would be
    replace aggreg_var1 = aggall_var1 if person = "X"
    But I was wondering if there is a way to do this for all variables at the same time?

    Many thanks,
    Alex

  • #2
    Welcome to Statalist.

    But I was wondering if there is a way to do this for all variables at the same time?
    Not that I know of, but it's possible to form a loop:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(person aggall_var1 aggall_var2 aggall_var3 aggreg_var1 aggreg_var2 aggreg_var3)
    99 1 2 3 4 5 6
    end
    
    forvalues i = 1/3{
        replace aggreg_var`i' = aggall_var`i' if person == 99
    }
    
    list
    Results:

    Code:
         +--------------------------------------------------------------------------+
         | person   aggall~1   aggall~2   aggall~3   aggreg~1   aggreg~2   aggreg~3 |
         |--------------------------------------------------------------------------|
      1. |     99          1          2          3          1          2          3 |
         +--------------------------------------------------------------------------+
    Also, it's if person == "X", two equal signs when it means logical equality.

    Comment

    Working...
    X