Hi Nick Cox. Thanks I can see there are problems. I think I have fixed line 7 based on your comments in #13, improved line 2 based on your comments in https://stackoverflow.com/questions/...range-function and applied this to lines 4 & 6.
It is ok to condition on inequality (in 3 & 4) as I did on equality (in 1 & 2) and then condition values from the inrange()? If not, can you please provide clues as to how to go about this? Also, is there a way to use inrange(`v', ...) to call different conditions as in 5 & 6 or do I need to condition separately? If so, some notes on where am I going wrong would be appreciated?
It is ok to condition on inequality (in 3 & 4) as I did on equality (in 1 & 2) and then condition values from the inrange()? If not, can you please provide clues as to how to go about this? Also, is there a way to use inrange(`v', ...) to call different conditions as in 5 & 6 or do I need to condition separately? If so, some notes on where am I going wrong would be appreciated?
Code:
foreach v in religb p_religb {
gen `v'2 = cond(religb == p_religb & inrange(`v', 2000, 2900), 1, ///
cond(religb == p_religb & inrange(`v', 1000, 6000) & !inrange(`v', 2000, 2900), 2, ///
cond(religb != p_religb & inrange(`v', 2000, 2900), 3, ///
cond(religb != p_religb & inrange(`v', 1000, 6000) & !inrange(`v', 2000, 2900), 4, ///
cond(p_religb == 7000 & inrange(religb, 2000, 2900), 5, ///
cond(p_religb == 7000 & inrange(religb, 1000, 6000) & !inrange(religb, 2000, 2900), 6, ///
cond(`v' == 7000, 7, .)))))))
}

Comment