Announcement

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

  • Wait only X seconds for console input by user

    Hello,
    When requesting user input (i.e., console input) during the execution of a do file, I would like Stata to wait up to X seconds for a user input (e.g., any character). If it receives a character input within X seconds, I would like the do file to continue execution, otherwise, I would like it to continue and use a default value for the missing entry.

    How can I accomplish this? Here is my failed attempt (with X=5):

    local y = clock("`c(current_time)'", "hms")
    local cmd ""
    local duration 0
    while ( strlen("`cmd'")==0 & `duration'<5000 ) {
    local duration = clock("`c(current_time)'", "hms") - `y'
    display "enter command: " _request(cmd)
    local cmd "$cmd"
    }
    if ( strlen("`cmd'")==0 ) {
    local cmd "default_value"
    }

    I assume this fails to function as I intended because the while loop cannot move past the display line until an entry is received from the user.

    I am starting to think that display _request() cannot be used in this way. I also do not see how to use a dialog box for this, but I have not read the documentation for more than 10 minutes.

    I would prefer not to use user-written commands, only those that come with my version of Stata. I am using Stata/MP version 17.0.

  • #2
    Originally posted by Tom Best View Post
    When requesting user input (i.e., console input) during the execution of a do file, I would like Stata to wait up to X seconds for a user input (e.g., any character). If it receives a character input within X seconds, I would like the do file to continue execution, otherwise, I would like it to continue and use a default value for the missing entry.
    Why? Why not pass an argument (or not pass an argument) to the do-file? If you want to set a value

    Code:
    do my_dofile cmd
    and if you want the default

    Code:
    do my_dofile
    In other words, the basic design strikes me a s somewhat unsual and complicated.

    Comment


    • #3
      Ah, yes, this simple suggestion solves my problem. I was too focused on the console input approach. Thank you!

      Comment


      • #4
        In the previous millennium I interacted with programs with dialog[ue]s like ,this:

        Do you want to enter another line of data? Type Y or N

        which got a bit wearing after about the third time.

        Twenty data points was a big dataset!

        Comment

        Working...
        X