Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Recursive Loop

    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.

    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
    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
    Last edited by Cydney Bruce; 19 Jul 2023, 05:28.

  • #2
    I don't follow this. sorry. But

    Code:
    egen abc = concat(a b c)
    takes up to 9 distinct values "111", "333".

    And if you just want a count. then

    Code:
    count if foo == 1 
    scalar sum = r(N)
    is simpler than running egen, total() repeatedly.

    Please give a data example and explain what new variables you want from it.

    Does produces mean products when mentioned?

    Comment


    • #3
      Hi Nick,

      Thank you for taking the time to respond, sorry yes I did mean products.

      Finding the product is not actually what I am trying to do here I was just using that to demonstrate the issue with the loop. A recursive function is one that calls itself, and the idea is that I need an unknown number of loops, so was hoping to using a recursive function to keep creating loops until the program hits the number it needs. The issue I have is that I am using n to track which loop I am in, however once the final loop completes and you back up a level, to do the second run of the loop, the value of n is still as it was in the final loop (in this case 3). This is causing the following pattern (using the above example):

      loop 1

      E1

      loop 2

      E11 E112 (which should be E12)

      loop 3

      E111, E112, E113

      Loop 3 will complete, and then return to loop 2 which has had it's n overwritten, producing the wrong output. I hope this adds some clarity?

      Best wishes,
      Cydney

      Comment


      • #4
        I am further ahead in knowing that you meant to talk about products in #1, but no further ahead in learning that your real example is different again -- and I can't see a statement of the real problem in #3.

        There is still no data example here either.

        I understand recursion but use it very rarely. I have written recursive code in Mata that works. Stata is a different ball game and use of local macros can complicate as much as it simplifies and I can see that in #1 your code is very much aware of that, even though I can't follow what you are trying to do.

        My experience is that people sometimes talk about recursion when the solution is just iterative code. I can't tell whether that applies here.

        I have to see this as an example of the X-Y problem https://xyproblem.info/, at least in terms of a reader like myself seeing a question on one level and wanting to know what the real underlying question is. So in this thread #1 was moderately abstract and #3 is going further in the same direction.

        Sorry, but I am not really helping and doubt that I can help at all without a completely different concrete question.

        Comment

        Working...
        X