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

st: Recursive program calls and the return list

From   Mike Lacy <>
Subject   st: Recursive program calls and the return list
Date   Mon, 20 Oct 2008 10:42:27 -0600

I've been experimenting with implementing recursive calls to a Stata program, and in particular, getting the program to store something in the return list at each call, so that at the end of program, the return list would contain r(something1) , r(something2), ..., r(somethingNumberOfCalls). I have experienced various difficulties in not clobbering the return list. Here's a simple example to demonstrate one of the problems I have had:

///  A "do nothing" recursion example; recur calls itself "stopit" times,
// and tries to return something each time in the locals something0,
// something1, something2, .., but only "something0" shows up in the return list
cap prog drop recr
prog recr, rclass
   args count stopit
   if `count' == `stopit' {
     return local something`count' = -99
   else {
     return local something`count' = uniform()
     local k = `count' +1
     recr `k' `stopit'
// Start count at 0, run 5 times
recr 0 5
ret list

        r(something0) : ".2769154107663781"

How could this be changed so as to accumulate something in the return list at each recursive call?


Mike Lacy
Director of Graduate Studies, Department of Sociology
Colorado State University, Fort Collins CO 80523 USA  (970) 491-6721 (voice)

*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index