Nick, thanks for your comments, immensely useful as usual. (Thanks for
your comments, Sergyi, too). This helped me out.
Thanks for pointing out the reg/regress issue. How would I specify the
-if- condition in a more flexible way, so that it accepts anything
between -reg- and -regress-? Is there some way of using wildcards?
Best, AC
On Sun, Mar 8, 2009 at 3:08 PM, Nick Cox <[email protected]> wrote:
> Orthogonal to this, this seems to commit any user wanting -regress- to abbreviating it as -reg-. If you are the user and want that, so be it.
>
> Nick
> [email protected]
>
> Nick Cox
>
> I wouldn't use globals here, or indeed almost anywhere else.
>
> Anyone watching needs to know that local macro 0 is special; it is born as whatever the user types after the command line. It can be redefined, as here with -gettoken-.
>
> A little inelegant, but practical, is to do something like this:
>
> program define mycmd
> local typed `0'
> gettoken subcmd 0: 0
> if "`subcmd'"=="reg" | "`subcmd'"=="areg" |
> "`subcmd'"=="xtreg" {
> mycmd_ols `typed'
> }
> else if "`subcmd'"=="ivreg" | "`subcmd'"=="xtivreg" |
> "`subcmd'"=="ivreg2" | "`subcmd'"=="xtivreg2" {
> mycmd_iv `typed'
> }
> else error 199
> end
>
> and then parse away.
>
> Alternatively, don't use -gettoken- at all. That sounds better.
>
> program define mycmd
> local subcmd : word 1 of `0'
> if "`subcmd'"=="reg" | "`subcmd'"=="areg" |
> "`subcmd'"=="xtreg" {
> mycmd_ols `0'
> }
> else if "`subcmd'"=="ivreg" | "`subcmd'"=="xtivreg" |
> "`subcmd'"=="ivreg2" | "`subcmd'"=="xtivreg2" {
> mycmd_iv `0'
> }
> else error 199
> end
>
> Nick
> [email protected]
>
> Augusto Cadenas
>
> I have a question about -gettoken- and programming in Stata. The stata
> help file suggests that -gettoken- can be used to create a two-word
> command. This is the example that is given:
>
> *** begin example ***
>
> program define mycmd
> gettoken subcmd 0: 0
> if "`subcmd'"=="list" {
> mycmd_l `0'
> }
> else if "`subcmd'"=="generate" {
> mycmd_g `0'
> }
> else error 199
> end
>
> program define mycmd_l
> ...
> end
>
> program define mycmd_g
> ...
> end
>
> *** end example ***
>
> I wonder how I could use the `subcmd' that has been determined by the
> first program, -mycmd-, within the sub-programs -mycmd_l- and
> -mycmd_g- without referring to it explicitly. To make a concrete
> example: In my case I want a program to do two similar, but slightly
> different things depending on whether I am doing an OLS regression or
> an IV regression. So the setup I have in mind is like:
>
> *** begin example ***
>
> program define mycmd
> gettoken subcmd 0: 0
> if "`subcmd'"=="reg" | "`subcmd'"=="areg" |
> "`subcmd'"=="xtreg" {
> mycmd_ols `0'
> }
> else if "`subcmd'"=="ivreg" | "`subcmd'"=="xtivreg" |
> "`subcmd'"=="ivreg2" | "`subcmd'"=="xtivreg2" {
> mycmd_iv `0'
> }
> else error 199
> end
>
> program define mycmd_ols
> ...
> `subcmd' `0'
> ...
> end
>
> program define mycmd_iv
> ...
> `subcmd' `0'
> ...
> end
>
> *** end example ***
>
> But this does not work, I guess because `subcmd' is not recognized
> within the next program. How do I get around that? It's two days I'm
> trying and I haven't found a solution. Thanks for any suggestions.
>
> *
> * 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/
>
*
* 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/