Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Temporary variables, ado files and subprograms

From   Maarten buis <>
Subject   Re: st: Temporary variables, ado files and subprograms
Date   Mon, 13 Sep 2010 10:56:46 +0000 (GMT)

--- On Mon, 13/9/10, Daniele Pacifico wrote:
> I am writing an ado file that - at a certain point - calls
> another ado file with a ML routine. Let's call these two files
> "ado1" and "ado2".
> The file "ado1" performs some computations and creates many
> temporary variables. 
> However, it is not an estimation command, it just creates a
> lot of variables and works with them to produce and display
> relevant results at the end..
> The file "ado2" is called within "ado1" and performs a
> basic ML estimation whose results are then needed in "ado1"..
> Ado2 should use the temporary variables created within
> "ado1" but - since they are "local" variables - Stata deletes
> all of them when "ado2" is called.. Is there a way to keep
> these temporary variables in memory without necessarily 
> using global macros?

Well that is not exactly correct: Stata deletes the temporary 
variables created in ado1 when ado1 ends. So if ado2 is called
by ado1, then the temporary variables created by ado1 are still
there. The only difficulty is to recover the names of those 
variables. You can use -syntax- for that to add an option or 
varlist in the syntax of ado2, which you can use to pass on the
names of the temporary variables in ado1. See the example below:

*---------------- begin example -------------------
// define program ado1
program drop _all

program ado2 
	syntax varlist
	foreach var of varlist `varlist' {
		replace `var' = runiform()

// add the variables you want to create
// but leave them empty (i.e. missing values)
drop _all
set obs 100
tempvar var1 var2 var3
forvalues i = 1/3 {
	gen `var`i'' = .
	local varl "`varl' `var`i''"

// use -ado2- to fill those variables
ado2 `varl'

// display summary statistics
foreach var of varlist `varl' {
	sum `var'
*------------------ end example --------------------

Hope this helps,

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen


*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index