Announcement

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

  • Create all variable-pair combinations

    Dear all,

    I have this dataset:

    Code:
    clear
    input str1(var1 var2)
    "A" "B"
    "A" "C"
    "B" "A"
    "C" "D"
    end
    and i want to create all possible variable-pairs combinations in order to get this:
    Code:
    clear
    input str1(var1 var2)
    "A" "A"
    "A" "B"
    "A" "C"
    "A" "D"
    "B" "A"
    "B" "B"
    "B" "C"
    "B" "D"
    "C" "A"
    "C" "B"
    "C" "C"
    "C" "D"
    "D" "A"
    "D" "B"
    "D" "C"
    "D" "D"
    end
    My initial thought was to use fillin. However, fillin will create a 3*4 dataset but I want a 4*4.
    Thank you in advance for the suggestions.

    Regards,
    Nikos

  • #2
    One more observation will suffice.

    Code:
    clear
    input str1(var1 var2)
    "A" "B"
    "A" "C"
    "B" "A"
    "C" "D"
    "D" "A" 
    end
    
    fillin var1 var2 
    count
    This device is documented at http://www.stata-journal.com/sjpdf.h...iclenum=dm0011

    Comment


    • #3
      Dear Nick,

      Thank you very much for the prompt reply.

      I am wondering if your solution can be automated as to handle the case that the two variables are more complicated in terms of the number of elements they include.

      Regards,
      Nikos

      Comment


      • #4
        Yes. Here's one way:

        Code:
        clear
        input str1(var1 var2)
        "A" "B"
        "A" "C"
        "B" "A"
        "C" "D"
        end
        
        stack var1 var2 var2 var1, into(var1 var2) clear 
        duplicates drop var1 var2 , force 
        fillin var1 var2 
        count

        Comment


        • #5
          Cheers!

          Comment

          Working...
          X