Announcement

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

  • How to prompt display request to re-request all information if part of entered answer string does not match approved list of words

    Hello,

    I am writing a program using Stata MP 14.2 that prompts the user for different pieces of information that will eventually populate a dataset of personnel details. For instance, the user will answer questions about which workers attended a weekly meeting, how long that weekly meeting was, when it was held, etc. These answers will be stored as globals and then used to populate new observations in a personnel dataset.

    As an example, one part of my code that currently works is question that prompts the user entering in the length of time for weekly meetings. It includes a check (albeit not foolproof, but good enough for my purposes) to make sure the date is entered in the format that I want. If it is not, it prompts the user to enter the date in again.

    Code:
     
    di "What date was the meeting? Enter it as in the following example: 10 Nov 2016" _request(meetingdate)
                
         while regexm("$meetingdate", "^[0-3]?[0-9]+ [A-Z][a-z][a-z] 20[1-2][0-9]$")!=1 {
            di "Your input is not in the following format: 10 Nov 2016.  Please try again" 
            di "What date was the meeting? Enter it as in the following example: 10 Nov 2016" _request(meetingdate)
         }
    This post is about another question that I need to ask regarding who was at the meeting -- specifically, how to include a similar check for accuracy in this as I do for the date question above.

    The question that the user is prompted with is below:

    Code:
    di "Who was not at the meeting?", _request(whonotmeeting)
    My goal is to compare the name(s) entered in response to this question and double check if they are spelled right according to my current list of workers. So, if my list of workers is:

    Code:
    global workers "harry hermoine ron ginny fred george"
    and "ron ginny draco" is entered in as a response to the the question about who was not at the meeting, I want the program to recognize that "draco" is not an accepted answer and say something like:

    Code:
    di "At least one of the names you entered is not correct.  Please reenter the list of who did not attend the meeting" 
    di "Who was not at the meeting?", _request(whonotmeeting)
    All of the solutions I've explored (counters comparing the number of names that match my $workers global to the number of names in the $whonotmeeting global, using a foreach loop to go through each name in the $whonotmeeting global and compare that to the full string of names in the $workers global) are able to recognize when a name doesn't match the full list of names that I am checking it against, but do not reset the question and name vetting process from the beginning (ie, for a reentering of all names).

    Any help is appreciated; happy to clarify the above as needed.

    Thanks,

    Sarah

  • #2
    You could try something like
    Code:
    local workers harry hermoine ron ginny fred george
    
    local error At least one of the names you entered is not correct. Please reenter the list of who did not attend the meeting
    while 1 {
        display in smcl as input "Who was not at the meeting?", _request(whonotmeeting)
        local correct : list global(whonotmeeting) in workers
        if !`correct' display in smcl as error "`error'"
        else continue, break
    }
    
    display in smcl as text "$whonotmeeting"
    
    exit
    Take a look at some of the other macro list directives (help macrolists), such as differencing, which could help you identify the incorrect members of the list that the user enters and at least point out which names don't match for a more helpful error message.

    Comment


    • #3
      This is exactly what I need, and has opened up a new world of macrolists to me. Thanks!

      Comment

      Working...
      X