Hi All,
I have been trying to use a recursive loop in Stata, but have ran into an issue holding a local macro. I have included some code below to demonstrate the program but the actual code is more complicated.
So this code is looking to find the produce of specific values in the variables. Three variables a, b and c can all take the values 1, 2 or 3. Hence all of the produces needed would be E111, E112, E113, E121, E122, E123 etc
The program aims to do this using a recursive loop, where it will call itself until it has reached the final variable.
So we take the first variable a, find out how many values == 1 then create a "product" of this with b == 1 and c == 1 (E111). The problem is, after the program has found E111, E112, E113 (essentially finishing the third loop) It returns to the second loop relying on the local macros E and PROD. These values change with each loop but I need to find a way to hold the values within a loop, so once the third loop completes and we look at b == 2, the local macro E should be E1 and PROD would be PROD11.
I'm sorry that feels a bit jumbled, it is a lot easier to explain with pointing and hand waving but the above code should run until it hits the issue so I am hoping this will make the issue a little more transparent.
I am hoping that someone would have advice on how to hold that value within the loop?
Thanks and best wishes,
Cydney
I have been trying to use a recursive loop in Stata, but have ran into an issue holding a local macro. I have included some code below to demonstrate the program but the actual code is more complicated.
Code:
/* Data Set Up */ clear set obs 1000 gen ax = uniform() gen bx = uniform() gen cx = uniform() gen a = 1 replace a = 2 if ax < 0.65 replace a = 3 if ax < 0.3 gen b = 1 replace b = 2 if bx < 0.65 replace b = 3 if bx < 0.3 gen c = 1 replace c = 2 if cx < 0.65 replace c = 3 if cx < 0.3 /* Recursive loop */ capture program drop recursive program define recursive args varlist n N E PROD local var: word `n' of `varlist' if `n' < `N' { foreach i of numlist 1/3 { egen `var'`i' = total(`var' == `i') gen `E'`i' = `PROD' * `var'`i' local E = "`E'`i'" gen PROD`n'`i' = `E' local PROD "PROD`i'" local n = `n' + 1 if `i' < 3 { recursive "`varlist'" `n' `N' `E' `PROD' } } } if `n' == `N' { foreach i of numlist 1/3 { egen `var'`i' = total(`var' == `i') gen `E'`i' = `PROD' * `var'`i' } } end recursive "a b c" 1 3 E 1
The program aims to do this using a recursive loop, where it will call itself until it has reached the final variable.
So we take the first variable a, find out how many values == 1 then create a "product" of this with b == 1 and c == 1 (E111). The problem is, after the program has found E111, E112, E113 (essentially finishing the third loop) It returns to the second loop relying on the local macros E and PROD. These values change with each loop but I need to find a way to hold the values within a loop, so once the third loop completes and we look at b == 2, the local macro E should be E1 and PROD would be PROD11.
I'm sorry that feels a bit jumbled, it is a lot easier to explain with pointing and hand waving but the above code should run until it hits the issue so I am hoping this will make the issue a little more transparent.
I am hoping that someone would have advice on how to hold that value within the loop?
Thanks and best wishes,
Cydney
Comment