When I was trying to using a question in the CLASS dataset, I met a multi-selection question(in dataset is a10201-a102015) where I wanted to identify each individual who selected only one option in this question, and I tried to identify which option he/she chose. So I used a nested while loop(code is below), but when I ran it in STATA, the ide told me that the variable live2 not found, I want know why this would happen. Thank you very much for your answer!
Code:
use "D:\CLASS\append_data\append_data.dta", clear
ren (a10210 a10211 a10212 a10213 a10214 a10215)(a102010 a102011 a102012 a102013 a102014 a102015)
local i 1
local n 2
local m 2
while `i' <= 15{
gen live`i' = 1 if a1020`i'==1
while `i' == 1{
replace live`i'= 0 if a10202/a102015==1
local i=`i'+1
}
while `i' == 2{
replace live`i'=0 if (a10201==1)&(a10203/a102015==1)
local i=`i'+1
}
while `i' == 3/14{
local `n' = `i'-1
local `m' = `i'+1
replace live`i'=0 if (a10201/a1020`n'==1)&(a1020`m'/a102015==1)
local i=`i'+1
}
while `i' == 15{
replace live`i'= 0 if a10201/a102014==1
local i=`i'+1
}
}

Comment