Search code examples
stata

How to make Stata wait for user input in each loop iteration?


I have a situation where users need to review/edit data by hand. I want to show them chunks of data at a time. I am doing this using a loop. My problem is to make the loop wait of the user to say that she's done with one chunk before moving to the next chunk.

I've tried set more on, pause on, and _request, but no luck. Here's an example of my set up with some fake data:

clear

set obs 100
gen id   = int( 5*uniform() )
gen var1 = uniform()

levelsof id, local(ids)
foreach id of local ids {

    edit if id == `id'
    
    /* 
       pause, 
       wait for user to hit any key in 
       the Command Window before moving
       to the next iteration
    */ 

}

Solution

  • Following Nick Cox's comment, here's my solution.

    clear
    
    set obs 100
    gen id   = int( 5*uniform() )
    gen var1 = uniform()
    
    levelsof id, local(ids)
    foreach id of local ids {
    
        edit if id == `id'
        
        * user must type _next_ in Command Window for next iteration
        local cmd 0
        while ("`cmd'" != "next") {
            
            display _request(cmd)
            local cmd "$cmd"
            
            /* here's the trick: the edits (replace var1 = bla in bla)
               are captured by _request(). I need to send the replace
               command back to stata */
            if ("`cmd'" != "next") {
                `cmd'
            }
        }
    }