Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Programming sub-routines


From   Nick Winter <[email protected]>
To   [email protected]
Subject   Re: st: Programming sub-routines
Date   Wed, 03 Nov 2004 16:12:49 -0500

You should read up on saving results in [U] 21.10.

The short version is that for returning value(s), usually you do an rclass program:

*------------beginning of mainprog.ado-------------------
program mainprog

...
subprog
... commands using r(whatever) , r(whichever) , etc....

...
end

program subprog, rclass
...

return scalar whatever = ...
return matrix whichever = ...
...
end
*------------end of mainprog.ado-------------------


For returning variables, you usually can send the name of the variable to be created or replaced as an argument to the subroutine:

*------------beginning of mainprog.ado-------------------
program mainprog

...
subprog `myvar'
...

end

program subprog
args variable

...

generate `variable' = ...

...

end
*------------end of mainprog.ado-------------------


Hope this helps
--Nick Winter


At 07:46 AM 11/4/2004 +1100, you wrote:

Dear Statalisters,

Before coming to Stata my programming was honed using Pascal and I'm
missing some of the tricks I used to access there. I would appreciate
any advice on a particular problem I have with Stata sub-routines.

I'm used to being able to modularise my programs (in Pascal) by having
lots of sub-routines for repetitive tasks, particularly functions
which carry out some kind of transformation. Some old database
languages (such as DBase) used to allow "user-defined" functions
for similar tasks.

I've tried to find the equivalent in Stata, but to no avail. If I use
a sub-routine inside a main program I can pass it arguments no
problem. But after doing some transformations on the arguments, how can I
send the results back to the main program? If I declare new local
macros to store the results, they disappear when I return to the main
program (because they're no longer in scope). If I declare global
macros, I can access the results back in the main program, but I'm
left with a lot of global macros. This appears to me bad practice, and
also requires lots of dropping of global macros on exit etc.

Am I missing something obvious? In Pascal terms arguments could be
"val" or "var" (the latter allowing for a return value). But in Stata all
arguments appear to only be "val". Is this right? Do I have any other
options? Any advice would be most appreciated.



--
Kind regards,
 Ian

=================================
Ian Watson
Senior Researcher
acirrt
University of Sydney
NSW 2006
Australia

Phone: 02 9351 5622
  Fax: 02 9351 5615

Email: [email protected]
  Web: www.acirrt.com
================================

*
*   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/
--------------------------------------------------------
Nicholas Winter 607.255.8819 t
Assistant Professor 607.255.4530 f
Department of Government [email protected] e
308 White Hall falcon.arts.cornell.edu/nw53 w
Cornell University
Ithaca, NY 14853-4601

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