Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Some questions about the nested while loop

    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
            }
        }

  • #2
    I have no idea what the CLASS dataset is. So here is example code that examines 3 0/1 variables and indicates which of the three is equal to 1 if precisely one of them is equal to 1.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(x1 x2 x3)
    0 0 0
    0 0 1
    0 1 0
    0 1 1
    1 0 0
    1 0 1
    1 1 0
    1 1 1
    end
    egen howmany = anycount(x1-x3), values(1)
    generate which = 0
    forvalue i=1/3 {
        replace which = `i' if howmany==1 & x`i'==1 
    }
    list, clean
    Code:
    . list, clean 
    
           x1   x2   x3   howmany   which  
      1.    0    0    0         0       0  
      2.    0    0    1         1       3  
      3.    0    1    0         1       2  
      4.    0    1    1         2       0  
      5.    1    0    0         1       1  
      6.    1    0    1         2       0  
      7.    1    1    0         2       0  
      8.    1    1    1         3       0
    Please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It is particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Comment

    Working...
    X