Ricardo Vale
Ricardo Vale

Reputation: 21

loop inside replace command

I need to build some conditional variables in my dataset.

I am doing it like this:

gen varx=0

replace varx=1 if resp_127=="A" | resp_128=="A"|resp_129=="A" |///
 resp_130=="A" | resp_131=="A" | resp_132=="A" ...

I would like to know if there is some way to put a loop in the condition, so I won't need to write resp_127 to resp_n again and again. I just want to write the loop that makes conditions from resp_127 to resp_140 automatically.

Upvotes: 2

Views: 42

Answers (1)

user4690969
user4690969

Reputation:

Something like this should do what you want.

generate varx = 0
forvalues num = 127/140 {
    replace varx=1 if resp_`num'=="A"
    }

Upvotes: 1

Related Questions