Announcement

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

  • -tuples- (SSC) and maximum number of local macros

    Is there a maximum number of local macros allowed in Stata? Or: What is the maximum number of local macros the SSC program tuples can generate?

    The following syntax creates the 80 local macros `tuple1' to `tuple80' as intended:
    Code:
    local px "05 10 15 20"
    local py "05 10 15 20"
    local r "50 55 60 65 70"
    
    * local py = "02.5 05 10 15 20 25 30 45 50 55"
    * local px = "02.5 05 10 15 20 25 30 45 50 55"
    * local r = "05 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95"
    
    local notupx2 ""
    tuples `px', min(2) max(2)
    forvalues t = 1/`ntuples' {
       local el = 0
       foreach n of local px {
          local ++el
          local num = `el'
          local tuple`t' = subinstr("`tuple`t''","`n'","`num'",.)
       }
       local tuple`t' = subinstr("`tuple`t''"," ","&",.)
       local notupx2 "`notupx2' !(`tuple`t'')"
    }
    * local notupx2 = strtrim("`notupx2'")
    local plus = `num'
    di "`notupx2'"
    
    local notupy2 ""
    tuples `py', min(2) max(2)
    forvalues t = 1/`ntuples' {
       local el = 0
       foreach n of local py {
          local ++el
          local num = `el'+`plus'
          local tuple`t' = subinstr("`tuple`t''","`n'","`num'",.)
       }
       local tuple`t' = subinstr("`tuple`t''"," ","&",.)
       local notupy2 "`notupy2' !(`tuple`t'')"
    }
    * local notupy2 = strtrim("`notupy2'")
    local plus = `num'
    
    local notupr2 ""
    tuples `r', min(2) max(2)
    forvalues t = 1/`ntuples' {
       local el = 0
       foreach n of local r {
          local ++el
          local num = `el'+`plus'
          local tuple`t' = subinstr("`tuple`t''","`n'","`num'",.)
       }
       local tuple`t' = subinstr("`tuple`t''"," ","&",.)
       local notupr2 "`notupr2' !(`tuple`t'')"
    }
    * local notupr = strtrim("`notupr2'")
    
    * ------------------------------------------------------------------------------
    
    local notupx3 = ""
    tuples `px', min(3) max(3)
    forvalues t = 1/`ntuples' {
       local el = 0
       foreach n of local px {
          local ++el
          local num = `el'
          local tuple`t' = subinstr("`tuple`t''","`n'","`num'",.)
       }
       local tuple`t' = subinstr("`tuple`t''"," ","&",.)
       local notupx3 "`notupx3' !(`tuple`t'')"
    }
    * local notupx3 = strtrim("`notupx3'")
    local plus = `num'
    
    local notupy3 = ""
    tuples `py', min(3) max(3)
    forvalues t = 1/`ntuples' {
       local el = 0
       foreach n of local py {
          local ++el
          local num = `el'+`plus'
          local tuple`t' = subinstr("`tuple`t''","`n'","`num'",.)
       }
       local tuple`t' = subinstr("`tuple`t''"," ","&",.)
       local notupy3 "`notupy3' !(`tuple`t'')"
    }
    * local notupy3 = strtrim("`notupy3'")
    local plus = `num'
    
    local notupr3 = ""
    tuples `r', min(3) max(3)
    forvalues t = 1/`ntuples' {
       local el = 0
       foreach n of local r {
          local ++el
          local num = `el'+`plus'
          local tuple`t' = subinstr("`tuple`t''","`n'","`num'",.)
       }
       local tuple`t' = subinstr("`tuple`t''"," ","&",.)
       local notupr3 "`notupr3' !(`tuple`t'')"
    }
    * local notupr3 = strtrim("`notupr3'")
    
    * ------------------------------------------------------------------------------
    local notupel "`notupx2' `notupx3' `notupy2' `notupy3' `notupr2' `notupr3'"
    macro drop _ntup* _tupl* _num _el _plus _notupx* _notupy* _notupr*
    * di "`notupel'"
    
    tuples `px' `py' `r' , min(3) max(3) conditionals(`notupel')
    macro dir
    However, if I de-comment lines 1-3 and comment out lines 5-7 instead, tuples first stops running and subequently Stata crashes. I know that 1,900 local macros are really a lot. If this is not possible, I will continue with a follow-up question explaining the purpose to see whether there is an alternative solution to my task.

  • #2
    Hmm, I don't know if there is a maximum number of macros at all. This page only talks about limits to the number of nested macros, and of the length of macro names and macro contents.

    Comment


    • #3
      Dirk, I wonder if the problem is with something other than -tuples- itself. I don't understand exactly what you want to do above (my fault; I'm not good at deciphering others' code), but I just did ran the following and my top tuple is tuple184756, no problem.

      Code:
      numlist "1/20"
      tuples `r(numlist)', min(10) max(10) 
      mac dir
      Your code ok for me with either set of locals. I have Stata 19.5 SE on a Windows machine, and I'm using tuples 4.2.0

      I have done quite a bit of fooling around with generating and processing combinations. Maybe your ultimate goal is like something I have done in a different way, so if you can't get your approach to work, I might be able to suggest something else. (For example, it might be easier to use a Stata combination generator that puts the combinations into a matrix or dataset one at a time?) If that's of interest, why don't you give a verbal description of what you are trying to do here?)

      Comment


      • #4
        I run a series of simulations with systematically going through combinations of x and y (ranging from .025 , .05 and then up to .55 in steps of .05) and r (ranging from .05 to .95 in steps of .05) and identified the seed number resulting to the closest values of the target values. Now I want to write code taking the seed from each of the simulated datasets (i.e. for each of the 1,900 combinations of x, y, and r). Therefore I have to specify commands taking these values for x, y, and r together with the seed number found. My intention was to use Stata to generate the code because writing up all combinations is a bit tedious.

        A bit more specific: In the end I want to arrive at one command line for each combination of x, y, and r where I will replace the value of the option seed(`i') by the seed number found in the previous simulations:
        Code:
        qui cap ovbd y x, means(M) corr(C) seed(`i') verbose
        I used his command in the previous simulation with 1,000 replications from which now I want to have only one specific seed number for each combination of x, y, and r (ovbd is from SSC)
        Last edited by Dirk Enzmann; 30 Aug 2025, 19:16. Reason: Added the second paragraph

        Comment


        • #5
          You can run code below to show that you can have (at least) 10M local macros and likely many more, so I doubt that there is any practical limit to the number of local macros for your purposes.

          Code:
          clear *
          cls
          
          forval i = 1/10000000 {
            local i`i'=`i'
            if mod(`i',100000)==0 mac list _i`i'
          }

          Comment


          • #6
            The problem may not be with the number of tuples, but with the number of conditions being put on the tuples. Where Stata seems to get stuck is line 76 of tuples.ado (I have version 4.2.0):
            Code:
            mata : conditionals_to_rpn("conditionals", `n')
            Specifically, it tries to run
            Code:
            mata : conditionals_to_rpn("conditionals", 39)
            where the local macro conditionals that is being provided as an input, looks like this:
            Code:
             !(9&10) !(8&10) !(8&9) !(7&10) !(7&9) !(7&8) !(6&10) !(6&9) !(6&8) !(6&7) !(5&10) !(5&9) !(5&8) !(5&7) !(5&6) !(4&10) !(4&9) !(4&8) !(4&7) !(4&6) !(4&5) !(3&
            > 10) !(3&9) !(3&8) !(3&7) !(3&6) !(3&5) !(3&4) !(2&10) !(2&9) !(2&8) !(2&7) !(2&6) !(2&5) !(2&4) !(2&3) !(1&10) !(1&9) !(1&8) !(1&7) !(1&6) !(1&5) !(1&4) !(1&3) !(1&2)
            >   !(8&9&10) !(7&9&10) !(7&8&10) !(7&8&9) !(6&9&10) !(6&8&10) !(6&8&9) !(6&7&10) !(6&7&9) !(6&7&8) !(5&9&10) !(5&8&10) !(5&8&9) !(5&7&10) !(5&7&9) !(5&7&8) !(5&6&10) !
            > (5&6&9) !(5&6&8) !(5&6&7) !(4&9&10) !(4&8&10) !(4&8&9) !(4&7&10) !(4&7&9) !(4&7&8) !(4&6&10) !(4&6&9) !(4&6&8) !(4&6&7) !(4&5&10) !(4&5&9) !(4&5&8) !(4&5&7) !(4&5&6)
            > !(3&9&10) !(3&8&10) !(3&8&9) !(3&7&10) !(3&7&9) !(3&7&8) !(3&6&10) !(3&6&9) !(3&6&8) !(3&6&7) !(3&5&10) !(3&5&9) !(3&5&8) !(3&5&7) !(3&5&6) !(3&4&10) !(3&4&9) !(3&4&8
            > ) !(3&4&7) !(3&4&6) !(3&4&5) !(2&9&10) !(2&8&10) !(2&8&9) !(2&7&10) !(2&7&9) !(2&7&8) !(2&6&10) !(2&6&9) !(2&6&8) !(2&6&7) !(2&5&10) !(2&5&9) !(2&5&8) !(2&5&7) !(2&5&
            > 6) !(2&4&10) !(2&4&9) !(2&4&8) !(2&4&7) !(2&4&6) !(2&4&5) !(2&3&10) !(2&3&9) !(2&3&8) !(2&3&7) !(2&3&6) !(2&3&5) !(2&3&4) !(1&9&10) !(1&8&10) !(1&8&9) !(1&7&10) !(1&7
            > &9) !(1&7&8) !(1&6&10) !(1&6&9) !(1&6&8) !(1&6&7) !(1&5&10) !(1&5&9) !(1&5&8) !(1&5&7) !(1&5&6) !(1&4&10) !(1&4&9) !(1&4&8) !(1&4&7) !(1&4&6) !(1&4&5) !(1&3&10) !(1&3
            > &9) !(1&3&8) !(1&3&7) !(1&3&6) !(1&3&5) !(1&3&4) !(1&2&10) !(1&2&9) !(1&2&8) !(1&2&7) !(1&2&6) !(1&2&5) !(1&2&4) !(1&2&3)  !(19&20) !(18&20) !(18&19) !(17&20) !(17&19
            > ) !(17&18) !(16&20) !(16&19) !(16&18) !(16&17) !(15&20) !(15&19) !(15&18) !(15&17) !(15&16) !(14&20) !(14&19) !(14&18) !(14&17) !(14&16) !(14&15) !(13&20) !(13&19) !(
            > 13&18) !(13&17) !(13&16) !(13&15) !(13&14) !(12&20) !(12&19) !(12&18) !(12&17) !(12&16) !(12&15) !(12&14) !(12&13) !(11&20) !(11&19) !(11&18) !(11&17) !(11&16) !(11&1
            > 5) !(11&14) !(11&13) !(11&12)  !(18&19&20) !(17&19&20) !(17&18&20) !(17&18&19) !(16&19&20) !(16&18&20) !(16&18&19) !(16&17&20) !(16&17&19) !(16&17&18) !(15&19&20) !(1
            > 5&18&20) !(15&18&19) !(15&17&20) !(15&17&19) !(15&17&18) !(15&16&20) !(15&16&19) !(15&16&18) !(15&16&17) !(14&19&20) !(14&18&20) !(14&18&19) !(14&17&20) !(14&17&19) !
            > (14&17&18) !(14&16&20) !(14&16&19) !(14&16&18) !(14&16&17) !(14&15&20) !(14&15&19) !(14&15&18) !(14&15&17) !(14&15&16) !(13&19&20) !(13&18&20) !(13&18&19) !(13&17&20)
            >  !(13&17&19) !(13&17&18) !(13&16&20) !(13&16&19) !(13&16&18) !(13&16&17) !(13&15&20) !(13&15&19) !(13&15&18) !(13&15&17) !(13&15&16) !(13&14&20) !(13&14&19) !(13&14&1
            > 8) !(13&14&17) !(13&14&16) !(13&14&15) !(12&19&20) !(12&18&20) !(12&18&19) !(12&17&20) !(12&17&19) !(12&17&18) !(12&16&20) !(12&16&19) !(12&16&18) !(12&16&17) !(12&15
            > &20) !(12&15&19) !(12&15&18) !(12&15&17) !(12&15&16) !(12&14&20) !(12&14&19) !(12&14&18) !(12&14&17) !(12&14&16) !(12&14&15) !(12&13&20) !(12&13&19) !(12&13&18) !(12&
            > 13&17) !(12&13&16) !(12&13&15) !(12&13&14) !(11&19&20) !(11&18&20) !(11&18&19) !(11&17&20) !(11&17&19) !(11&17&18) !(11&16&20) !(11&16&19) !(11&16&18) !(11&16&17) !(1
            > 1&15&20) !(11&15&19) !(11&15&18) !(11&15&17) !(11&15&16) !(11&14&20) !(11&14&19) !(11&14&18) !(11&14&17) !(11&14&16) !(11&14&15) !(11&13&20) !(11&13&19) !(11&13&18) !
            > (11&13&17) !(11&13&16) !(11&13&15) !(11&13&14) !(11&12&20) !(11&12&19) !(11&12&18) !(11&12&17) !(11&12&16) !(11&12&15) !(11&12&14) !(11&12&13)  !(38&39) !(37&39) !(37
            > &38) !(36&39) !(36&38) !(36&37) !(35&39) !(35&38) !(35&37) !(35&36) !(34&39) !(34&38) !(34&37) !(34&36) !(34&35) !(33&39) !(33&38) !(33&37) !(33&36) !(33&35) !(33&34)
            >  !(32&39) !(32&38) !(32&37) !(32&36) !(32&35) !(32&34) !(32&33) !(31&39) !(31&38) !(31&37) !(31&36) !(31&35) !(31&34) !(31&33) !(31&32) !(30&39) !(30&38) !(30&37) !(3
            > 0&36) !(30&35) !(30&34) !(30&33) !(30&32) !(30&31) !(29&39) !(29&38) !(29&37) !(29&36) !(29&35) !(29&34) !(29&33) !(29&32) !(29&31) !(29&30) !(28&39) !(28&38) !(28&37
            > ) !(28&36) !(28&35) !(28&34) !(28&33) !(28&32) !(28&31) !(28&30) !(28&29) !(27&39) !(27&38) !(27&37) !(27&36) !(27&35) !(27&34) !(27&33) !(27&32) !(27&31) !(27&30) !(
            > 27&29) !(27&28) !(26&39) !(26&38) !(26&37) !(26&36) !(26&35) !(26&34) !(26&33) !(26&32) !(26&31) !(26&30) !(26&29) !(26&28) !(26&27) !(25&39) !(25&38) !(25&37) !(25&3
            > 6) !(25&35) !(25&34) !(25&33) !(25&32) !(25&31) !(25&30) !(25&29) !(25&28) !(25&27) !(25&26) !(24&39) !(24&38) !(24&37) !(24&36) !(24&35) !(24&34) !(24&33) !(24&32) !
            > (24&31) !(24&30) !(24&29) !(24&28) !(24&27) !(24&26) !(24&25) !(23&39) !(23&38) !(23&37) !(23&36) !(23&35) !(23&34) !(23&33) !(23&32) !(23&31) !(23&30) !(23&29) !(23&
            > 28) !(23&27) !(23&26) !(23&25) !(23&24) !(22&39) !(22&38) !(22&37) !(22&36) !(22&35) !(22&34) !(22&33) !(22&32) !(22&31) !(22&30) !(22&29) !(22&28) !(22&27) !(22&26)
            > !(22&25) !(22&24) !(22&23) !(21&39) !(21&38) !(21&37) !(21&36) !(21&35) !(21&34) !(21&33) !(21&32) !(21&31) !(21&30) !(21&29) !(21&28) !(21&27) !(21&26) !(21&25) !(21
            > &24) !(21&23) !(21&22)  !(37&38&39) !(36&38&39) !(36&37&39) !(36&37&38) !(35&38&39) !(35&37&39) !(35&37&38) !(35&36&39) !(35&36&38) !(35&36&37) !(34&38&39) !(34&37&39
            > ) !(34&37&38) !(34&36&39) !(34&36&38) !(34&36&37) !(34&35&39) !(34&35&38) !(34&35&37) !(34&35&36) !(33&38&39) !(33&37&39) !(33&37&38) !(33&36&39) !(33&36&38) !(33&36&
            > 37) !(33&35&39) !(33&35&38) !(33&35&37) !(33&35&36) !(33&34&39) !(33&34&38) !(33&34&37) !(33&34&36) !(33&34&35) !(32&38&39) !(32&37&39) !(32&37&38) !(32&36&39) !(32&3
            > 6&38) !(32&36&37) !(32&35&39) !(32&35&38) !(32&35&37) !(32&35&36) !(32&34&39) !(32&34&38) !(32&34&37) !(32&34&36) !(32&34&35) !(32&33&39) !(32&33&38) !(32&33&37) !(32
            > &33&36) !(32&33&35) !(32&33&34) !(31&38&39) !(31&37&39) !(31&37&38) !(31&36&39) !(31&36&38) !(31&36&37) !(31&35&39) !(31&35&38) !(31&35&37) !(31&35&36) !(31&34&39) !(
            > 31&34&38) !(31&34&37) !(31&34&36) !(31&34&35) !(31&33&39) !(31&33&38) !(31&33&37) !(31&33&36) !(31&33&35) !(31&33&34) !(31&32&39) !(31&32&38) !(31&32&37) !(31&32&36)
            > !(31&32&35) !(31&32&34) !(31&32&33) !(30&38&39) !(30&37&39) !(30&37&38) !(30&36&39) !(30&36&38) !(30&36&37) !(30&35&39) !(30&35&38) !(30&35&37) !(30&35&36) !(30&34&39
            > ) !(30&34&38) !(30&34&37) !(30&34&36) !(30&34&35) !(30&33&39) !(30&33&38) !(30&33&37) !(30&33&36) !(30&33&35) !(30&33&34) !(30&32&39) !(30&32&38) !(30&32&37) !(30&32&
            > 36) !(30&32&35) !(30&32&34) !(30&32&33) !(30&31&39) !(30&31&38) !(30&31&37) !(30&31&36) !(30&31&35) !(30&31&34) !(30&31&33) !(30&31&32) !(29&38&39) !(29&37&39) !(29&3
            > 7&38) !(29&36&39) !(29&36&38) !(29&36&37) !(29&35&39) !(29&35&38) !(29&35&37) !(29&35&36) !(29&34&39) !(29&34&38) !(29&34&37) !(29&34&36) !(29&34&35) !(29&33&39) !(29
            > &33&38) !(29&33&37) !(29&33&36) !(29&33&35) !(29&33&34) !(29&32&39) !(29&32&38) !(29&32&37) !(29&32&36) !(29&32&35) !(29&32&34) !(29&32&33) !(29&31&39) !(29&31&38) !(
            > 29&31&37) !(29&31&36) !(29&31&35) !(29&31&34) !(29&31&33) !(29&31&32) !(29&30&39) !(29&30&38) !(29&30&37) !(29&30&36) !(29&30&35) !(29&30&34) !(29&30&33) !(29&30&32)
            > !(29&30&31) !(28&38&39) !(28&37&39) !(28&37&38) !(28&36&39) !(28&36&38) !(28&36&37) !(28&35&39) !(28&35&38) !(28&35&37) !(28&35&36) !(28&34&39) !(28&34&38) !(28&34&37
            > ) !(28&34&36) !(28&34&35) !(28&33&39) !(28&33&38) !(28&33&37) !(28&33&36) !(28&33&35) !(28&33&34) !(28&32&39) !(28&32&38) !(28&32&37) !(28&32&36) !(28&32&35) !(28&32&
            > 34) !(28&32&33) !(28&31&39) !(28&31&38) !(28&31&37) !(28&31&36) !(28&31&35) !(28&31&34) !(28&31&33) !(28&31&32) !(28&30&39) !(28&30&38) !(28&30&37) !(28&30&36) !(28&3
            > 0&35) !(28&30&34) !(28&30&33) !(28&30&32) !(28&30&31) !(28&29&39) !(28&29&38) !(28&29&37) !(28&29&36) !(28&29&35) !(28&29&34) !(28&29&33) !(28&29&32) !(28&29&31) !(28
            > &29&30) !(27&38&39) !(27&37&39) !(27&37&38) !(27&36&39) !(27&36&38) !(27&36&37) !(27&35&39) !(27&35&38) !(27&35&37) !(27&35&36) !(27&34&39) !(27&34&38) !(27&34&37) !(
            > 27&34&36) !(27&34&35) !(27&33&39) !(27&33&38) !(27&33&37) !(27&33&36) !(27&33&35) !(27&33&34) !(27&32&39) !(27&32&38) !(27&32&37) !(27&32&36) !(27&32&35) !(27&32&34)
            > !(27&32&33) !(27&31&39) !(27&31&38) !(27&31&37) !(27&31&36) !(27&31&35) !(27&31&34) !(27&31&33) !(27&31&32) !(27&30&39) !(27&30&38) !(27&30&37) !(27&30&36) !(27&30&35
            > ) !(27&30&34) !(27&30&33) !(27&30&32) !(27&30&31) !(27&29&39) !(27&29&38) !(27&29&37) !(27&29&36) !(27&29&35) !(27&29&34) !(27&29&33) !(27&29&32) !(27&29&31) !(27&29&
            > 30) !(27&28&39) !(27&28&38) !(27&28&37) !(27&28&36) !(27&28&35) !(27&28&34) !(27&28&33) !(27&28&32) !(27&28&31) !(27&28&30) !(27&28&29) !(26&38&39) !(26&37&39) !(26&3
            > 7&38) !(26&36&39) !(26&36&38) !(26&36&37) !(26&35&39) !(26&35&38) !(26&35&37) !(26&35&36) !(26&34&39) !(26&34&38) !(26&34&37) !(26&34&36) !(26&34&35) !(26&33&39) !(26
            > &33&38) !(26&33&37) !(26&33&36) !(26&33&35) !(26&33&34) !(26&32&39) !(26&32&38) !(26&32&37) !(26&32&36) !(26&32&35) !(26&32&34) !(26&32&33) !(26&31&39) !(26&31&38) !(
            > 26&31&37) !(26&31&36) !(26&31&35) !(26&31&34) !(26&31&33) !(26&31&32) !(26&30&39) !(26&30&38) !(26&30&37) !(26&30&36) !(26&30&35) !(26&30&34) !(26&30&33) !(26&30&32)
            > !(26&30&31) !(26&29&39) !(26&29&38) !(26&29&37) !(26&29&36) !(26&29&35) !(26&29&34) !(26&29&33) !(26&29&32) !(26&29&31) !(26&29&30) !(26&28&39) !(26&28&38) !(26&28&37
            > ) !(26&28&36) !(26&28&35) !(26&28&34) !(26&28&33) !(26&28&32) !(26&28&31) !(26&28&30) !(26&28&29) !(26&27&39) !(26&27&38) !(26&27&37) !(26&27&36) !(26&27&35) !(26&27&
            > 34) !(26&27&33) !(26&27&32) !(26&27&31) !(26&27&30) !(26&27&29) !(26&27&28) !(25&38&39) !(25&37&39) !(25&37&38) !(25&36&39) !(25&36&38) !(25&36&37) !(25&35&39) !(25&3
            > 5&38) !(25&35&37) !(25&35&36) !(25&34&39) !(25&34&38) !(25&34&37) !(25&34&36) !(25&34&35) !(25&33&39) !(25&33&38) !(25&33&37) !(25&33&36) !(25&33&35) !(25&33&34) !(25
            > &32&39) !(25&32&38) !(25&32&37) !(25&32&36) !(25&32&35) !(25&32&34) !(25&32&33) !(25&31&39) !(25&31&38) !(25&31&37) !(25&31&36) !(25&31&35) !(25&31&34) !(25&31&33) !(
            > 25&31&32) !(25&30&39) !(25&30&38) !(25&30&37) !(25&30&36) !(25&30&35) !(25&30&34) !(25&30&33) !(25&30&32) !(25&30&31) !(25&29&39) !(25&29&38) !(25&29&37) !(25&29&36)
            > !(25&29&35) !(25&29&34) !(25&29&33) !(25&29&32) !(25&29&31) !(25&29&30) !(25&28&39) !(25&28&38) !(25&28&37) !(25&28&36) !(25&28&35) !(25&28&34) !(25&28&33) !(25&28&32
            > ) !(25&28&31) !(25&28&30) !(25&28&29) !(25&27&39) !(25&27&38) !(25&27&37) !(25&27&36) !(25&27&35) !(25&27&34) !(25&27&33) !(25&27&32) !(25&27&31) !(25&27&30) !(25&27&
            > 29) !(25&27&28) !(25&26&39) !(25&26&38) !(25&26&37) !(25&26&36) !(25&26&35) !(25&26&34) !(25&26&33) !(25&26&32) !(25&26&31) !(25&26&30) !(25&26&29) !(25&26&28) !(25&2
            > 6&27) !(24&38&39) !(24&37&39) !(24&37&38) !(24&36&39) !(24&36&38) !(24&36&37) !(24&35&39) !(24&35&38) !(24&35&37) !(24&35&36) !(24&34&39) !(24&34&38) !(24&34&37) !(24
            > &34&36) !(24&34&35) !(24&33&39) !(24&33&38) !(24&33&37) !(24&33&36) !(24&33&35) !(24&33&34) !(24&32&39) !(24&32&38) !(24&32&37) !(24&32&36) !(24&32&35) !(24&32&34) !(
            > 24&32&33) !(24&31&39) !(24&31&38) !(24&31&37) !(24&31&36) !(24&31&35) !(24&31&34) !(24&31&33) !(24&31&32) !(24&30&39) !(24&30&38) !(24&30&37) !(24&30&36) !(24&30&35)
            > !(24&30&34) !(24&30&33) !(24&30&32) !(24&30&31) !(24&29&39) !(24&29&38) !(24&29&37) !(24&29&36) !(24&29&35) !(24&29&34) !(24&29&33) !(24&29&32) !(24&29&31) !(24&29&30
            > ) !(24&28&39) !(24&28&38) !(24&28&37) !(24&28&36) !(24&28&35) !(24&28&34) !(24&28&33) !(24&28&32) !(24&28&31) !(24&28&30) !(24&28&29) !(24&27&39) !(24&27&38) !(24&27&
            > 37) !(24&27&36) !(24&27&35) !(24&27&34) !(24&27&33) !(24&27&32) !(24&27&31) !(24&27&30) !(24&27&29) !(24&27&28) !(24&26&39) !(24&26&38) !(24&26&37) !(24&26&36) !(24&2
            > 6&35) !(24&26&34) !(24&26&33) !(24&26&32) !(24&26&31) !(24&26&30) !(24&26&29) !(24&26&28) !(24&26&27) !(24&25&39) !(24&25&38) !(24&25&37) !(24&25&36) !(24&25&35) !(24
            > &25&34) !(24&25&33) !(24&25&32) !(24&25&31) !(24&25&30) !(24&25&29) !(24&25&28) !(24&25&27) !(24&25&26) !(23&38&39) !(23&37&39) !(23&37&38) !(23&36&39) !(23&36&38) !(
            > 23&36&37) !(23&35&39) !(23&35&38) !(23&35&37) !(23&35&36) !(23&34&39) !(23&34&38) !(23&34&37) !(23&34&36) !(23&34&35) !(23&33&39) !(23&33&38) !(23&33&37) !(23&33&36)
            > !(23&33&35) !(23&33&34) !(23&32&39) !(23&32&38) !(23&32&37) !(23&32&36) !(23&32&35) !(23&32&34) !(23&32&33) !(23&31&39) !(23&31&38) !(23&31&37) !(23&31&36) !(23&31&35
            > ) !(23&31&34) !(23&31&33) !(23&31&32) !(23&30&39) !(23&30&38) !(23&30&37) !(23&30&36) !(23&30&35) !(23&30&34) !(23&30&33) !(23&30&32) !(23&30&31) !(23&29&39) !(23&29&
            > 38) !(23&29&37) !(23&29&36) !(23&29&35) !(23&29&34) !(23&29&33) !(23&29&32) !(23&29&31) !(23&29&30) !(23&28&39) !(23&28&38) !(23&28&37) !(23&28&36) !(23&28&35) !(23&2
            > 8&34) !(23&28&33) !(23&28&32) !(23&28&31) !(23&28&30) !(23&28&29) !(23&27&39) !(23&27&38) !(23&27&37) !(23&27&36) !(23&27&35) !(23&27&34) !(23&27&33) !(23&27&32) !(23
            > &27&31) !(23&27&30) !(23&27&29) !(23&27&28) !(23&26&39) !(23&26&38) !(23&26&37) !(23&26&36) !(23&26&35) !(23&26&34) !(23&26&33) !(23&26&32) !(23&26&31) !(23&26&30) !(
            > 23&26&29) !(23&26&28) !(23&26&27) !(23&25&39) !(23&25&38) !(23&25&37) !(23&25&36) !(23&25&35) !(23&25&34) !(23&25&33) !(23&25&32) !(23&25&31) !(23&25&30) !(23&25&29)
            > !(23&25&28) !(23&25&27) !(23&25&26) !(23&24&39) !(23&24&38) !(23&24&37) !(23&24&36) !(23&24&35) !(23&24&34) !(23&24&33) !(23&24&32) !(23&24&31) !(23&24&30) !(23&24&29
            > ) !(23&24&28) !(23&24&27) !(23&24&26) !(23&24&25) !(22&38&39) !(22&37&39) !(22&37&38) !(22&36&39) !(22&36&38) !(22&36&37) !(22&35&39) !(22&35&38) !(22&35&37) !(22&35&
            > 36) !(22&34&39) !(22&34&38) !(22&34&37) !(22&34&36) !(22&34&35) !(22&33&39) !(22&33&38) !(22&33&37) !(22&33&36) !(22&33&35) !(22&33&34) !(22&32&39) !(22&32&38) !(22&3
            > 2&37) !(22&32&36) !(22&32&35) !(22&32&34) !(22&32&33) !(22&31&39) !(22&31&38) !(22&31&37) !(22&31&36) !(22&31&35) !(22&31&34) !(22&31&33) !(22&31&32) !(22&30&39) !(22
            > &30&38) !(22&30&37) !(22&30&36) !(22&30&35) !(22&30&34) !(22&30&33) !(22&30&32) !(22&30&31) !(22&29&39) !(22&29&38) !(22&29&37) !(22&29&36) !(22&29&35) !(22&29&34) !(
            > 22&29&33) !(22&29&32) !(22&29&31) !(22&29&30) !(22&28&39) !(22&28&38) !(22&28&37) !(22&28&36) !(22&28&35) !(22&28&34) !(22&28&33) !(22&28&32) !(22&28&31) !(22&28&30)
            > !(22&28&29) !(22&27&39) !(22&27&38) !(22&27&37) !(22&27&36) !(22&27&35) !(22&27&34) !(22&27&33) !(22&27&32) !(22&27&31) !(22&27&30) !(22&27&29) !(22&27&28) !(22&26&39
            > ) !(22&26&38) !(22&26&37) !(22&26&36) !(22&26&35) !(22&26&34) !(22&26&33) !(22&26&32) !(22&26&31) !(22&26&30) !(22&26&29) !(22&26&28) !(22&26&27) !(22&25&39) !(22&25&
            > 38) !(22&25&37) !(22&25&36) !(22&25&35) !(22&25&34) !(22&25&33) !(22&25&32) !(22&25&31) !(22&25&30) !(22&25&29) !(22&25&28) !(22&25&27) !(22&25&26) !(22&24&39) !(22&2
            > 4&38) !(22&24&37) !(22&24&36) !(22&24&35) !(22&24&34) !(22&24&33) !(22&24&32) !(22&24&31) !(22&24&30) !(22&24&29) !(22&24&28) !(22&24&27) !(22&24&26) !(22&24&25) !(22
            > &23&39) !(22&23&38) !(22&23&37) !(22&23&36) !(22&23&35) !(22&23&34) !(22&23&33) !(22&23&32) !(22&23&31) !(22&23&30) !(22&23&29) !(22&23&28) !(22&23&27) !(22&23&26) !(
            > 22&23&25) !(22&23&24) !(21&38&39) !(21&37&39) !(21&37&38) !(21&36&39) !(21&36&38) !(21&36&37) !(21&35&39) !(21&35&38) !(21&35&37) !(21&35&36) !(21&34&39) !(21&34&38)
            > !(21&34&37) !(21&34&36) !(21&34&35) !(21&33&39) !(21&33&38) !(21&33&37) !(21&33&36) !(21&33&35) !(21&33&34) !(21&32&39) !(21&32&38) !(21&32&37) !(21&32&36) !(21&32&35
            > ) !(21&32&34) !(21&32&33) !(21&31&39) !(21&31&38) !(21&31&37) !(21&31&36) !(21&31&35) !(21&31&34) !(21&31&33) !(21&31&32) !(21&30&39) !(21&30&38) !(21&30&37) !(21&30&
            > 36) !(21&30&35) !(21&30&34) !(21&30&33) !(21&30&32) !(21&30&31) !(21&29&39) !(21&29&38) !(21&29&37) !(21&29&36) !(21&29&35) !(21&29&34) !(21&29&33) !(21&29&32) !(21&2
            > 9&31) !(21&29&30) !(21&28&39) !(21&28&38) !(21&28&37) !(21&28&36) !(21&28&35) !(21&28&34) !(21&28&33) !(21&28&32) !(21&28&31) !(21&28&30) !(21&28&29) !(21&27&39) !(21
            > &27&38) !(21&27&37) !(21&27&36) !(21&27&35) !(21&27&34) !(21&27&33) !(21&27&32) !(21&27&31) !(21&27&30) !(21&27&29) !(21&27&28) !(21&26&39) !(21&26&38) !(21&26&37) !(
            > 21&26&36) !(21&26&35) !(21&26&34) !(21&26&33) !(21&26&32) !(21&26&31) !(21&26&30) !(21&26&29) !(21&26&28) !(21&26&27) !(21&25&39) !(21&25&38) !(21&25&37) !(21&25&36)
            > !(21&25&35) !(21&25&34) !(21&25&33) !(21&25&32) !(21&25&31) !(21&25&30) !(21&25&29) !(21&25&28) !(21&25&27) !(21&25&26) !(21&24&39) !(21&24&38) !(21&24&37) !(21&24&36
            > ) !(21&24&35) !(21&24&34) !(21&24&33) !(21&24&32) !(21&24&31) !(21&24&30) !(21&24&29) !(21&24&28) !(21&24&27) !(21&24&26) !(21&24&25) !(21&23&39) !(21&23&38) !(21&23&
            > 37) !(21&23&36) !(21&23&35) !(21&23&34) !(21&23&33) !(21&23&32) !(21&23&31) !(21&23&30) !(21&23&29) !(21&23&28) !(21&23&27) !(21&23&26) !(21&23&25) !(21&23&24) !(21&2
            > 2&39) !(21&22&38) !(21&22&37) !(21&22&36) !(21&22&35) !(21&22&34) !(21&22&33) !(21&22&32) !(21&22&31) !(21&22&30) !(21&22&29) !(21&22&28) !(21&22&27) !(21&22&26) !(21
            > &22&25) !(21&22&24) !(21&22&23)"'
            Last edited by Hemanshu Kumar; 30 Aug 2025, 21:58.

            Comment


            • #7
              Mike Lacy : Thanks for offering assistance! However, I can solve my task without the use of tuples by determining the optimal seed number directly after each simulation block and writing a respective string with the command that uses this seed number into a string variable in a second frame. Subsequently I can use that variable (containing the Stata code) further.

              There is still the question why Stata crashes when using tuples as in the example in #1. But this is a question to the programmers (Joseph Luchman, daniel klein, and Nick Cox) or StataCorp. And a secondary question here is whether the conditionals() option could more effectively be used instead of the rather complicated creation of the local macro `notupel` (should have better been named `notuples`) (and to reproduce the problem lines 1-3 should be "commented out", not "de-commented" as I wrote in #1).

              Comment


              • #8
                Joseph Luchman is the first author of tuples. I started it off but long since Joseph and Daniel have been taking it further and I've not looked at the code for several years.

                Comment


                • #9
                  I've had a chance to look into the issue. This is neither a limit on local macros, nor a problem with Stata or Mata. It's a problem with our Python code, specifically with how we parse and reconstruct the conditionals(). In a nutshell, we convert the conditionals() option to Reverse Polish notation, then rebuild a logical string in infix notation and evaluate it for each tuple. Given very lengthy input, this reconstruction can produce deeply nested parentheses that the Python parser can't handle.

                  If he wishes, Dirk Enzmann may specify the nopython option to obtain the results he wants. However, a more direct and much more readable solution to his particular problem seems to be:
                  Code:
                  local P = "02.5 05 10 15 20 25 30 45 50 55"
                  local R = "05 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95"
                  
                  foreach x of local P {
                      
                      foreach y of local P {
                          
                          foreach r of local R {
                              
                              display "`x' `y' `r'" // <- or whatever you want to do
                              
                          }
                          
                      }
                      
                  }
                  We may look into improving the Python code in a future update, though it's not at the top of my priority list.

                  Comment


                  • #10
                    Sorry to be late in following up, but I hope it's better late than never. I'd suggest that uses of -tuples- that generate large numbers of macros might also be approached by processing the combinations (tuples) one at a time, which is a common way to handle the output of combination generators. There's a nice and fast way to do this in Stata with the -mmsubset()- command of the user-written -moremata- package by Ben Jann (-ssc describe moremata-). (While Mata contains a built-in -jumble()- function that randomly permutes a vector, there's no combination generator in it, to my knowledge.) Here are two minimal examples of how to use -mmsubset()- for whatever use they might be to people who like me don't use Mata very much.

                    1) Generate all possible combinations of 6 choose 3 and feed each such list of integers, one list at a time, as a string to a Stata command. (This is like the creation of locals, as in the current thread.)

                    Code:
                    cap prog drop MyCmd
                    prog MyCmd
                       syntax, mycomb(string)
                       di "In Stata: `mycomb' "
                    end
                    //
                    mata:
                    info = mm_subsetsetup(6,3 )
                    while ((comb=mm_subset(info)) != J(0,1,.)) { 
                      // comb'  mm_subset creates this
                      combstring = invtokens(strofreal(comb'))
                      cmd = "MyCmd," + " mycomb(" + combstring + ")" 
                      stata(cmd)
                    }
                    end
                    The preceding, without the display, took a few second on my machine for 20 choose 10.

                    2) Use each combination as a list of indices that point to observations in the data set. One then returns this to Stata as a variable. One use-case for this would be to generate all possible permutations of a binary grouping variable, which could be used for a fully exact permutation test. All I do here is list each permutation of the group variable.
                    Code:
                    clear
                    set obs 6
                    gen byte group = (_n <= 3)
                    local N = 6 
                    local ng = 3
                    gen byte rangroup = .
                    //
                    mata:
                    info = mm_subsetsetup(`N', `ng' )
                    while ((index=mm_subset(info)) != J(0,1,.)) { 
                       rangroup = J(`N',1, 0) 
                       for (i = 1; i <= `ng'; i++) {
                          rangroup[index[i]] = 1
                       }
                       st_store(., "rangroup", rangroup)
                       stata("list rangroup")
                    }
                    end
                    For me, this latter code took less than a second for 20 choose 10 (leaving out the "list" command).

                    Comment


                    • #11
                      Thanks to Kit Baum, an update for tuples is now available from the SSC.

                      The update includes revised Python code, which now produces the desired results from #1, although it is still slower than the nopython (Mata) implementation.

                      The update also implements Mike Lacy's suggestion in #10 to obtain the tuples one at a time. While this approach avoids storing all tuples in local macros, the speed gain is often smaller than one might expect. Mike's first example of 20 choose 10 can (and could for quite some time) be replicated with tuples as follows:
                      Code:
                      numlist "1/20"
                      tuples `r(numlist)' , min(10) max(10)
                      forvalues i = 1/`ntuples' {
                          MyCmd , mycomb(`tuple`i'')
                      }
                      Although all 184,756 tuples are generated before being processed, the difference in running time is negligible.

                      Still, generating tuples one at a time saves memory and enables workflows that might be difficult to replicate with the two-step approach. The updated tuples package now includes Mata functions to do that. Here's a replication of Mike's second example of 6 choose 3:
                      Code:
                      // setup copied verbatim
                      clear
                      set obs 6
                      gen byte group = (_n <= 3)
                      local N = 6
                      local ng = 3
                      gen byte rangroup = .
                      // replicate mm_subset() with new tuplesget() functions
                      mata:
                      info = tuplessetup((1..`N'), `ng' )
                      while ( !tuplesdone(info) ) {
                         index = tuplesget(info)
                         rangroup = J(`N',1, 0)
                         for (i = 1; i <= `ng'; i++) {
                            rangroup[index[i]] = 1
                         }
                         st_store(., "rangroup", rangroup)
                         stata("list rangroup")
                      }
                      end
                      For more details and additional examples, type
                      Code:
                      help mata tuplesget()
                      after installing the updated tuples.
                      Last edited by daniel klein; 06 Oct 2025, 11:42.

                      Comment

                      Working...
                      X