Statalist The Stata Listserver


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

Re: st: Program within a Program


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: Program within a Program
Date   Wed, 1 Nov 2006 14:09:13 -0500

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

On 11/1/06, Kyle C Longest <[email protected]> wrote:
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.

Here is the neccesary part of the code I have so far: capture program
drop testprog
program define testprog
syntax varlist(numeric) [if] [, cluster(varname) modtest(namelist)
mod2(varlist) RANks]
marksample touse
local v `varlist'
local y : word 1 of `v'

where -modtest- defines the appropriate model test to display and
-mod2- tells it what the independent variables in the second model
should be.

Here's the program for the -spread- option (which needs to be an
available -modtest-)

local check: list posof "spread" in modtest
if `check' > 0 {
capture program drop spread
program define spreadprog, rclass

...spread program generates variables "spread" and "spread2" *based on
`varlist' and `mod2', as well as returning the difference between the
two....

end
}

local check: list posof "spread" in settest
if `check' > 0 {
bootstrap diff = r(diff) spread=r(spread) spread2=r(spread2), reps(50): spread
di %9.3f _b[spread] , %9.3f _b[spread2], %9.2f _b[diff]/_se[diff],
%9.3f 2*(1-normal(_b[diff]/_se[diff]))
}

...rest of testprog continues...

I've tried a couple of ways of doing this and keep running into
troubles. 1)I tried embedding it within the -testprog- but because I
put end after the -spreadprog- it does not include the -bootstrap-
command as part of the -testprog-. 2) If I wait and put end at the very
end, it returns me to Stata without doing the -bootstrap-, which needs
the -spreadprog- to `end' to work properly. 3) I tried defining
-spreadprog- outside of -testprog- and then simply calling it in
bootstrap (within -testprog-), but when I do this -spreadprog- does not
seem to be able to find `varlist' or `mod2' as defined by the
-testprog- so it can not run properly.

[Note: The -spreadprog- program works properly when I run it as a stand
alone program and manually enter the variables to be used]

I hope this is clear, but just to reiterate I need to run a program (as
an option) within a master program that relies on locals from the
master program.

Any suggestions or advice would be greatly appreciated, and I can
provide more info if that is needed.

Thanks again,
Kyle C. Longest
Phd Student - Dept of Sociology
Univ. of North Carolina - Chapel Hill
[email protected]
*
*   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/

*
*   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