Announcement

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

  • replace groups of variables' value

    Dear Stata users,

    Suppose we have groups of variables whose values are coded as zero or one. Now we want to replace values of zero to two. It seems that we have to use a loop to come at this in Stata. For example the Code1 below. I want to write a program to automate this operation without writing loops each time, see the Program1 below. The program, I call it -replaceg-, seems work well, and would anyone in this forums help me to debug this little program and improve it. Thank you!
    Code:
    // Code1
    input x y z
    0 1 1
    0 1 0
    1 0 1
    1 0 0
    1 1 0
    1 1 1
    0 0 1
    0 0 0
    1 0 1
    end
    
    foreach v of varlist x y z {
     replace `v'=2 if `v'==0
     }
    Code:
    // Program1
    program define replaceg
    
    syntax varlist, old(real) new(real)
     tokenize `varlist'
     foreach v of varlist `varlist' {
       replace `v'=`new' if `v'==`old'
     }
      
    end

  • #2
    Code:
    help recode

    Comment


    • #3
      Joseph Coveney, thank you very much, I do forgot to use -recode-.

      Comment

      Working...
      X