简体   繁体   中英

Stata - Generate all possible combinations

I need to find all possible combinations of the following variables, each containing a X number of observations

Variable Obs

  • Black 1
  • Pink 2
  • Yellow 6
  • Red 15
  • Green 17

eg (black, pink), (black, pink, yellow), (black, pink, yellow, red), (red, green).... Order is not important, so I must delete all the combinations that contain the same elements (black, pink) and (pink, black).

Also, at the end I would need to calculate the number of total observations per each combination.

What is the fastest method, which is also less prone to errors?

I read about Tuples but I am not able to write the code myself.

You can use tuples (to install ssc install tuples ), like this:

tuples black pink yellow red green 
scalar black=1
scalar pink=2
scalar yellow=6
scalar red=15
scalar green=17

forvalues i = 1/`ntuples' {
    scalar sum = 0
    foreach n of local tuple`i' {
        scalar sum = sum + `n'
    }
    di "`tuple`i'': " sum
}

Output:

green: 17
red: 15
yellow: 6
pink: 2
black: 1
red green: 32
yellow green: 23
yellow red: 21
pink green: 19
pink red: 17
pink yellow: 8
black green: 18
black red: 16
black yellow: 7
black pink: 3
yellow red green: 38
pink red green: 34
pink yellow green: 25
pink yellow red: 23
black red green: 33
black yellow green: 24
black yellow red: 22
black pink green: 20
black pink red: 18
black pink yellow: 9
pink yellow red green: 40
black yellow red green: 39
black pink red green: 35
black pink yellow green: 26
black pink yellow red: 24
black pink yellow red green: 41

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM