Following is a response from Senior DHS Stata Specialist, Tom Pullum:
There are two ways to do this. The easy way is with the first two lines below. The second way is to loop through the b variables, as you were trying to do. The last three lines will confirm that you get the same results with both methods.
gen boys1=v202+v204+v206
gen girls1=v203+v205+v207
ren b*_0* b*_*
local li=1
while `li'<=20 {
gen boy_`li'=1 if b4_`li'==1
gen girl_`li'=1 if b4_`li'==2
local li=`li'+1
}
egen boys2=rowtotal(boy_*)
egen girls2=rowtotal(girl_*)
correlate boys*
correlate girls*
summarize boys* girls*
[Updated on: Tue, 29 January 2019 08:18]
Report message to a moderator