Announcement

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

  • pairwise combination.

    Dear All, I find this question here (https://bbs.pinggu.org/thread-7317333-1-1.html). Suppose that the data is
    Code:
    clear
    input str6(var1 var2 var3 var4 var5)
    "A01B1"  "A01C05" "A01C11" "A01M21" "E02F3"
    "A01H11" "B25D1" "E23P15" "" ""
    end
    and the purpose is to obtain pairwise combination as (the desired results)
    Code:
    clear
    input str6 (var1 var2 var3 var4 var5) str200 combine
    "A01B1"  "A01C05" "A01C11" "A01M21" "E02F3" "A01B1.A01C5;A01B1.A01C11;A01B1.A01M21; A01B1.E02F3;A01C05.A01C11;A01C05.A01M21;A01C05.E02F3;A01C11.A01M21;A01C05.E02F3;A01C11.A01M21;A01C11.E02F3;A01M21.E02F3"
    "A01H11" "B25D1" "E23P15" "" "" "A01H11.B25D1;A01H11.E23P15;B25D1.E23P15"
    end
    Any suggestions are highly appreciated.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Hello River Huang. This was an interesting problem to tackle. I am sure I do not have the most elegant solution, but below you can find some sample code that I believe will accomplish your desired goal, as well as a sample data set attached on which to run it before modifying the code to fit your data set.

    You will need to change the range "v1-v4" to match the variables from which you want to create the pairwise combinations. You can obviously also rename the comb variable, which will contain the long string of pairwise combinations, to however you want it to read in your data set.

    Code:
    import excel "testing.xlsx", firstrow    // Make sure you edit the file path as needed
    
    gen comb = ""     // Variable that will contain all pairwise combinations, excluding combination of var with itself
    local chk = 0     // Variable that will keep track of, "check", the iteration of the following outside loop
    
    foreach var1 of varlist v1-v4 {
    
        local chk = `chk' + 1
        local chk2 = `chk'    // This second check variable will tell us how many variables to ignore before adding more combinations    
        
        foreach var2 of varlist v1-v4 {
        
            // The following if statements ensure that no combination is added to comb before you get to new ones
            if `chk2' == 1 {
                replace comb = comb + `var1' + `var2' + ";" if `var1' != `var2' & `var1' != "" & `var2' != ""
                // If statements here ensure we don't add a combination of duplicate strings nor a combination where one of the strings is missing
            }
            if `chk2' > 1 {
                local chk2 = `chk2' - 1
            }
        }
    }
    Attached Files

    Comment


    • #3
      See also https://www.statalist.org/forums/for...many-variables
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        the question is not completely clear to me but it is possible that the user-written command -tuples- will do what you want; it is available at SSC

        Comment


        • #5
          Thank you for Prof. Huang's help (Prof. Huang sees my Chinese post and lists here), and thank you for all your suggestions. I learn a lot and really like Nate's solution!

          Comment


          • #6
            Many thanks for all your answers.
            Ho-Chuan (River) Huang
            Stata 19.0, MP(4)

            Comment

            Working...
            X