Statalist The Stata Listserver


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

RE: st: Program within a Program


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Program within a Program
Date   Wed, 1 Nov 2006 22:00:13 -0000

Austin gave exactly the advice I would like to have given, and 
what I have to say is just an expansion of one of his details. 

Setting aside whether one program calls another, consider 

program kyle 
	...
	<reference to `varlist'> 
end 

program austin
	...
	< reference to `varlist'>
	... 
end 

The key point is that each `varlist' is -local-, and the 
name really does mean what it says, i.e. a local macro
is local to a "program", broad sense. "Programs",  
broad sense, certainly include -program-s, strict 
sense: they also include do files, the contents of
the do-file editor, and the main interactive session. 

In short, one `varlist' cannot be seen or referred to 
_inside_ another program. 

In essence, this is much of what makes Stata programming
possible at all: if we had to know, and to worry about, 
any other uses of the same local macro name within 
other parts of Stata it would be very difficult to 
get anything done except by continually inventing
new outlandish names for things, which in itself 
would also be difficult to follow. 

So, the main way of letting one program know about 
another's `varlist' is to pass that `varlist' 
as an argument. In fact, the other program never
ever sees the `varlist', except as interpreted by 
Stata. 

Nick 
[email protected] 

Austin Nichols
 
> 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.
 
> > 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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index