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" <austinnichols@gmail.com>
To   "Kyle C Longest" <klongest@email.unc.edu>, statalist@hsphsun2.harvard.edu
Subject   Re: st: Program within a Program
Date   Wed, 1 Nov 2006 16:17:20 -0500

You can pass options to your second program the same way you would to
your main program:

prog testsub
syntax varlist(numeric)  [, mod2(varlist)]
sub `varlist', mod2(`mod2')
di "sub reports varlist (`mod2') forms " r(N) " groups"
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

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

Remember locals can only be seen within the program or do-file where
they are defined, and Statalist members can only comment on examples
using data that is easily available to all copies of Stata.

On 11/1/06, Kyle C Longest <klongest@email.unc.edu> wrote:
Austin,

I'm not sure if it's proper etiquette to reply individually but it
seems like it might be easier in this case.

Yes, I believe I am trying to do exactly what you described, and I took
a look through the manual and the example you pointed out. But I'm
still not 100% sure how to pass on arguments (or maybe I am and should
just try it).

So from my example how would I pass on `varlist' (coming from syntax in
master program) and `mod2' (from the options in syntax in master
program) macros to the subprogram (-spreadprog) from the master program.

Here's what I derived and if it's correct no need for a full reply. If
I save the programs together in the same .ado file, then when I call to
`varlist' and/or `mod2' will it recognize/use them from the master
program. [I tried to do just this but I was only working in .do files.
So will the transfer to .ado make the difference here?]

Thank you for all of your help, it is truly appreciated,
Kyle

Quoting Austin Nichols <austinnichols@gmail.com>:

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
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index