Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Program within a Program


From   Gary Longton <[email protected]>
To   [email protected]
Subject   Re: st: Program within a Program
Date   Wed, 01 Nov 2006 14:57:31 -0800

Kyle C Longest wrote, in part:

> I have a question about using a program within a program. I have
> created a program and have added a set of options to it. Everything is
> working fine, except one of my options contains another program I wrote
> within it, and I can not figure out how to get it to work properly.
> This particular option runs a bootstrap on the program within it.
> . . .

and Austin Nichols replied:

> Kyle--
> Do I understand that you are trying to define a -program- within the
> definition of another program in a -program- -end- block? The right
> way to program is to write a bunch of separate programs, save them to
> a file called something.ado, and to pass parameters from one program
> to another as appropriate using -args- or -syntax- or the like. See
> the [P] manual and have a look at a good example:
> . viewsource ivreg.ado
>

Kyle subsequently asked about passing local macro arguments from a main program to a subprogram, and about wrapping them in an .ado vs. .do file. Austin then provided a nice illustration with example program -testsub- , which calls subprogram -sub-. And Nick elaborated on the locality of local macros in a program.

If I understand Kyle's original program and question correctly, there may be one additional consideration that will have an effect here, and that is Kyle's intended use of the -bootstrap- command in the main program. To borrow from Austin's example, if the programs -testsub- and -sub- are both defined in the program file testsub.ado, the -sub- program can be called directly from the -testsub- program, but it canNOT be called from -testsub- as a command argument to the -bootstrap- command.

I have borrowed and modified Austin's example to fit what I perceive to be Kyle's intended use of a local subprogram, i.e. as command argument to -bootstrap-. The following should not work:

----------
*** beginning of testsub.ado *****
prog testsub
syntax varlist(numeric) [, mod2(varlist)]
bootstrap ngrp = r(N) : sub `varlist', mod2(`mod2')
end

prog sub, rclass
syntax varlist(numeric) [, mod2(varlist)]
tempvar g
qui egen `g' = group(`mod2')
qui tab `g'
ret scalar N = `=r(r)'
end
*** end of testsub.ado *******

sysuse auto
testsub mpg, mod2(rep78)
tab rep78
-----------

-bootstrap-, called from within a user defined program, will not recognize a locally defined subprogram. I know this only because I attempted to write a program to do just this recently, and was assured by Stata Technical support that I could not (though I made an appeal for a future change to what I guess to be a correctable limitation of -bootstrap-). The suggested way around this was to define the called program (-sub-) as a separate program outside of the main program (-testsub.ado- in this case).

Wrapping the two programs above in a .do file rather than an .ado file should, on the other hand, work. -sub- will in this case be defined as separate program unrelated to -testsub- rather than as a subprogram.

- Gary

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




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