Chris
Chris

Reputation: 1

Simple multiple variable replace

I want a simple way to replace the values of several variables (let's say varlist A) conditional on the values of other variables. In my case to every name of each variable in A there corresponds a modified name in B as folllows:

A (var1 var2 var3 ...)

B (_var1 _var2 _var3 ...)

So I would like to replace the value of var1 by that of _var1 if var1 is missing

Upvotes: 0

Views: 49

Answers (1)

Nick Cox
Nick Cox

Reputation: 37183

Various ways; here is one.

  forval j = 1/3 { 
        replace var`j' = _var`j' if missing(var`j') 
  } 

For a tutorial on such loops, see http://www.stata-journal.com/sjpdf.html?articlenum=pr0005

(LATER) "My real problem is different...."

 foreach v in p503 p605 p201c {
     replace `v' = _`v' if missing(`v') 
 } 

If you read the article I suggested you will learn that forval and foreach can take loops over various kinds of list.

Upvotes: 2

Related Questions