Announcement

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

  • Combination of values from observation who sum equal another value (10 in this case)

    I intend to generate a combination of all possible values whose sum equal 10. Few such combination are produced manually here from the example data

    Code:
    (1,2,3,4),(1,9),(2,8),(3,7),(4,6),(1,3,6)
    and so on.......

    Example data:
    Code:
    clear
    input long var1
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
     1
    14
    15
    16
    17
    18
    19
    10
    end

    Is there anyway to go about this programmatically and list such all combination who value equal another value (in this case 10).

    Thank you in advance.




    Last edited by Ataullah Khan; 23 Aug 2022, 02:12.

  • #2
    Are negative values allowed? Or zero? Otherwise

    Code:
    clear
    set obs 10 
    gen y = _n 
    gen x = 10 - y
    may get you started. Depending on the next stage it may be sufficient to loop over one variable and just construct its complement in 10 on the fly.

    Comment


    • #3
      Thanks Nick. The code you suggested above only work for pairs whose sum is 10.

      However, in this case, I want all possible subset of observations in my list (this subset can have more than 2 values), whose sum is equal a hypothetical value, 10.
      Though this is a toy example data. Actual data is more complex but the idea remain the same.

      Also negative values and zeroes are not allowed. I just need the subset of my list with all possible combination of numbers in it.

      Comment


      • #4
        Sorry, but I am still guessing at exactly what you want, as without any context I don't understand what you want to do with what you find, or to do about what you don't find.

        By inspection all integers from 1 to 9 occur with var1 in your example data. Therefore the possible pairs 1 and 9, 2 and 8, 3 and 7, 4 and 6 can all be found. A further solution 5 and 5 doesn't arise in your example because 5 only occurs once.

        Comment


        • #5
          Consider this code. It does more or less what you want. The variable x is supposed to contain your original numbers; feel free to change appropriately. I think the code works well as long as these numbers are unique.

          Code:
          clear
          
          local wantsum = 10
          
          set obs 19
          gen x = _n
          tempfile base
          save `base'
          
          * determine maximum size of groups
              gen n = _n
              sort x
              gen s = (sum(x) > `wantsum')
              sum n if s == 0
              local maxsize `r(max)'
          
          * generate combinations of each group size, and save those that add up to the required number
              local files
              
              forval i = 2/`maxsize' {
                  use `base', clear
                  forval j = 1/`i' {
                      gen _x_`j' = x
                  }
          
                  fillin _x_*
                  forval j = 1/`=`i'-1' {
                      drop if _x_`j' >= _x_`=`j'+1'
                  }
                  
                  egen sum = rowtotal(_x_*)
                  keep if sum == `wantsum'
                  drop x sum _fillin
                  tempfile f`i'
                  save `f`i''
                  local files `files' `f`i''        
              }
          
          clear
          append using `files'
          
          egen combo = concat(_x_*), punct(",")
          replace combo = "(" + subinstr(combo,",.","",.) + ")"
          
          noi dis "The combinations that add up to `wantsum' are:"
          forval i = 1/`=_N' {
          noi dis "`=combo[`i']'" " " _continue
          }
          noi dis _n
          Note that this is a fairly brute-force method and that the -fillin- command will make your computer freeze for long periods if the sizes of combinations become large (say more than 6 or 7).

          The output is:

          Code:
          The combinations that add up to 10 are:
          (1,9) (2,8) (3,7) (4,6) (1,2,7) (1,3,6) (1,4,5) (2,3,5) (1,2,3,4)
          Last edited by Hemanshu Kumar; 23 Aug 2022, 06:03.

          Comment


          • #6
            #2 and #4 were stupid on my part. You never said pairs. Sorry about that.

            Comment


            • #7
              Thank you Hemanshu Kumar. This is what I needed. It worked perfect.

              Comment

              Working...
              X