Announcement

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

  • Loop for determining where a respondent abandoned the survey

    Hello,

    I am working with survey data and I want to be able to analyze at which points in the survey respondents are abandoning the survey. My idea was to designate certain missing values as "stop points" if all of the variables ahead of it are also missing. So, say my data looks like this below. For observation 2, for instance, I would like to signal variable AJ as the place where the respondent stopped answering the survey (and recode it to be .a, or something like that).
    Click image for larger version

Name:	Screen Shot 2020-11-02 at 11.49.37 AM.png
Views:	1
Size:	37.0 KB
ID:	1579983 I'm wondering if someone can help me code a loop to apply this to all of my variables. How do I tell Stata to look for missing values in all of the variables AHEAD of a variable?
    Thanks in advance for any help!

    P.S. I'm so sorry for the screenshot, I can't get the formatting right on the data example I'm trying to post.
    Last edited by LiMaria Lopez; 02 Nov 2020, 09:51.

  • #2
    Here's one approach.


    Code:
    clear 
    set obs 10 
    local j = 1 
    foreach v in A B C D E F G { 
        gen `v' = 1 if runiform() > `j'/9 
        local ++j 
    }
    
    gen long id = _n 
    save original_today, replace  
    
    keep id ? 
    rename ? y#, addnumber 
    reshape long y, i(id) j(which) 
    
    bysort id : egen last = max(cond(y < ., which, .))   
    bysort id : replace y = .a if which > last 
    reshape wide y, i(id) j(which) 
     
    merge 1:1 id using original_today 
    drop _merge 
    l id A-G last y* 
    
        +--------------------------------------------------------------------------+
         | id   A   B   C   D   E   F   G   last   y1   y2   y3   y4   y5   y6   y7 |
         |--------------------------------------------------------------------------|
      1. |  1   .   1   1   .   1   1   .      6    .    1    1    .    1    1   .a |
      2. |  2   1   .   .   1   1   .   1      7    1    .    .    1    1    .    1 |
      3. |  3   1   1   1   .   .   .   .      3    1    1    1   .a   .a   .a   .a |
      4. |  4   1   1   .   1   1   .   .      5    1    1    .    1    1   .a   .a |
      5. |  5   1   .   .   1   1   .   1      7    1    .    .    1    1    .    1 |
         |--------------------------------------------------------------------------|
      6. |  6   1   1   1   1   .   1   .      6    1    1    1    1    .    1   .a |
      7. |  7   1   1   1   .   .   .   .      3    1    1    1   .a   .a   .a   .a |
      8. |  8   1   1   .   1   .   .   .      4    1    1    .    1   .a   .a   .a |
      9. |  9   1   1   1   .   1   .   .      5    1    1    1    .    1   .a   .a |
     10. | 10   1   1   1   .   1   .   .      5    1    1    1    .    1   .a   .a |
         +--------------------------------------------------------------------------+
    Code:
    
    


    Comment


    • #3
      Hi Nick, thanks so much for this.

      Sorry it took me a few days to respond, I got caught up with a different task. I'm running into a problem here, which is that some of my variables are actually string variables (and cannot be converted). How would that change the code you recommended?

      Thanks again.

      Comment


      • #4
        #2 is based on the assumption that all variables are numeric. The more general problem of a mix of numeric and string is addressed in this code. As missing strings are already empty, it is not clear what you want to do with them, but here I just use text ".a".


        Code:
        * fake dataset
        
        clear
        set obs 10
        set seed 2803
        local j = 1
        foreach v in A B C D E F G {
            if mod(`j', 2) gen `v' = 1 if runiform() >  `j'/9
            else gen `v' = "x" if runiform() >  `j'/9
            local ++j
        }
        
        gen last_nm = 0 
        
        local j = 1
        foreach v in A B C D E F G  {
            replace last_nm = `j' if !missing(`v')
            local ++j
        }
        
        local j = 1
        foreach v in A B C D E F G {
            capture confirm numeric var `v'
            if _rc == 0 gen new`j' = cond(`j'  > last_nm, .a, `v')  
            else gen new`j' = cond(`j' >  last_nm, ".a", `v')  
            local ++j
        }
        
        l  A-G last_nm new*

        Comment

        Working...
        X