Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: Re: Prompt user of ado file for input & output file names & path
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
st: RE: Re: Prompt user of ado file for input & output file names & path
Date
Thu, 7 Jul 2011 10:50:05 +0100
In essence, the bottom line here is "no pain, no gain". If you want an interface that copes well with irrelevant or incorrect responses, you need to learn dialog programming. If you want very simple request commands, they won't work well at coping with such responses.
But you can do something like this.
local valid 0
while !`valid' {
<request using -display->
<process request; if valid, set -local valid 1->
}
In essence, you can get your command to play moderately dumb until the user does what is needed. Of course, you can be as friendly as you like by adding informative error messages.
Nick
[email protected]
P.S. I think that most active participants on Statalist would also sign up to being "within a very tight corporate work-schedule in mid-career". It makes us reluctant to write personal tutorials rehashing what is already very well documented!
Partho Sarkar
Thank you so much, Eric! And sorry for the late response-I had given
up hope of getting a more explicit answer here, and looked it up quite
laboriously elsewhere. I had discovered about the -display,
_request(), and also -db use for the browser option. But your answer
is nonetheless very useful.
Nick is of course right to suggest that one should first search
through the help files, manuals etc, and I usually do that quite a
bit. But I am having to pick up Stata on the go, as it were, within a
very tight corporate work-schedule in mid-career, hence the naivete of
some of my questions. (I suppose the same goes for many of the
"newbies" here!)
Thank you again for taking the time to give a detailed and very
helpful answer. And in case you or someone would care to help with a
few more basic queries, here are a few wrinkles I am struggling with
right now (comments in do file style)
1. *An attempt with _request:
. noi di " ENTER SOURCE FILE NAME --> " _request(answer)
di "File name =$answer "
/*But what if what if the user inadvertently hits the <enter> key w/o
entering any name? to take care of that,I tried the next 3 lines of
code, the idea being to repeatedly prompt the user till he does enter
a valid name:*/
if "${answer}" == "" {
di " PLEASE ENTER SOURCE FILE NAME --> " _request(answer
continue}
/* but this does nor work- it produces the following:
PLEASE ENTER SOURCE FILE NAME --> .
invalid syntax
r(198);*/.
2. Further, suppose I want to prompt the user for a csv file to use
with the insheet command. I can use
db insheet
but this allows various ascii file formats, and the rest of my ado
file will only work if the file is csv, and with comma (not tab!)
separators. How to ensure this? [A successful choice via the window
menu would produce a result as below:
insheet using "C:\Users\indicus-laptop\Desktop\prices.csv", comma]
Thanks again
Partho
Eric A. Booth wrote [4 Jul 2011 17:19:10 +0000]:<>
As Nick suggested, -dialog- boxes are what you want (especially to
allow users to navigate folders); however, I also like to use
-display, request()- (see -help display-) and thought others may find
it useful:
******************
di `" TYPE THE FILEPATH INTO THE COMMAND WINDOW AND THEN PRESS ENTER
"' _request(ans)
**puts path in global macro $ans
**echo that back to the user:
window stopbox note `"PATH ENTERS AS : $ans"'
*or:
di `"PATH ENTERED AS: $ans"'
******************
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/