John Antonakis <John.Antonakis@unil.ch>:
You might also want to step through various conditions and then issue
a more informative error message at the end, e.g. consider adapting
this approach:
cap prog drop john
prog john, rclass
version 12
loc err 0
args no_vars df N chi
loc w: word count `no_vars' `df' `N' `chi'
if `w'>4 {
loc err 1
di in red "More than 4 arguments given"
}
foreach v in no_vars df N chi {
cap confirm integer number `=``v'''
if _rc!=0 {
loc err 1
di in red "Arguments must be integers."
}
else if ``v''<0 {
loc err 1
di in red "Arguments must be positive integers."
}
}
if `err'==1 {
di in red "Please enter data after the command" _c
di in red " john as follows: vars df N chi"
exit 198
}
end
On Thu, Mar 21, 2013 at 5:49 PM, daniel klein <klein.daniel.81@gmail.com> wrote:
> John,
>
> I think I would address this with something like this
>
> cap pr drop john
> pr john
> vers 12.1
>
> args a b c d
> if !inrange(`: word count `0'', 1, 4) {
> di as err "Please enter data after " ///
> "the command john as follows: vars df N chi"
> e 198
> }
> numlist "`0'" ,int r(>0)
>
> end
>
> Best
> Daniel
> --
> Hi:
>
> I am writing a program and would like Stata to return an error if an
> argument is missing or there are too many of them.
>
>
> For example, if an argument is not an integer, the below will return an error:
>
>
> program define john, rclass
> version 12
> args no_vars df N chi
>
> if `no_vars'<0 | `df'<0 | `N'<0 | `chi'<0 {
> di in red "You have to use integers."
> exit 498
> }
> end
>
> Now, how do I make Stata display:
>
> di in red "Please enter data after the command john as follows: vars df N chi"
>
>
> .....in cases where (a) there is a missing value (i.e., the user
> missed putting 4 numbers) and (b) if the user provides too many
> numbers (i.e., 5 instead of 4).
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/