Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Re: Two-word commands with gettoken


From   Sergiy Radyakin <serjradyakin@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Re: Two-word commands with gettoken
Date   Sun, 8 Mar 2009 18:40:59 -0400

Dear Augusto,

after -gettoken- the local `0' does not contain `subcmd' anymore, so

1) just put it back there:

gettoken subcmd 0: 0
local 0 `"`subcmd'`0'"'  // <<<---- new line
if ....

or

2) instead of mycmd_ols `0' call mycmd_ols `subcmd' `0' (same effect,
do -gettoken- again in mycmd_ols to obtain subcommand), or
3) you might want to get rid of the two other procedures and add your
analysis commands within the brunching in mycmd, where subcmd is
visible.

Best regards,
   Sergiy Radyakin



On Sat, Mar 7, 2009 at 6:58 PM, Augusto Cadenas
<aug.cadenas@googlemail.com> wrote:
> Martin, thank you. Sure, the -if- command is all on one line. Thanks
> for the globals suggestion. So for example I could do
>
> *** begin code ***
>
>       program define mycmd
>               gettoken subcmd 0: 0
>                                if "`subcmd'"=="reg" | "`subcmd'"=="areg" | "`subcmd'"=="xtreg"{
>                                        global command `"`subcmd'"'
>                                        mycmd_ols `0'
>               }
>                                        ... ...
>       end
>
>       program define mycmd_ols
>               di " $command"
>       end
>
> ...
>
> *** end code ***
>
> or how is the use of global within -gettoken- to be understood? I tried
>
> ***
>               gettoken (global) subcmd 0: 0
> ***
>
> but it did not work.
>
> Thanks,
>
> AC
>
> On Sat, Mar 7, 2009 at 4:36 PM, Martin Weiss <martin.weiss1@gmx.de> wrote:
>> <>
>>
>> Are you positively sure that your -if- conditions are all on one logical
>> line? That is what I stumbled upon. Still, `subcmd' is -local- to -mycmd-
>> which you can easily see from a -display- in mycmd_ols
>>
>> *****
>>       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
>>               di "`subcmd'"
>>       end
>>
>>       program define mycmd_iv
>>       di "`subcmd'"
>>       end
>> ********
>>
>> -h gettoken- also talks about the possibility to put its results into a
>> -global- which should be a way out for you...
>>
>>
>> HTH
>> Martin
>> _______________________
>> ----- Original Message ----- From: "Augusto Cadenas"
>> <aug.cadenas@googlemail.com>
>> To: <statalist@hsphsun2.harvard.edu>
>> Sent: Saturday, March 07, 2009 9:09 PM
>> Subject: st: Two-word commands with gettoken
>>
>>
>>> Hello,
>>>
>>> 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.
>>>
>>> AC
>>> *
>>> *   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/
>>
>
> *
> *   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index