Announcement

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

  • Count number of variable combinations

    I want to create a variable that counts the number of combinations between two variables. I have a variable 'buyerID' and 'sellerID' for 100,000 transactions. I want to know how many times a certain buyer and seller have done business together.

    Thank you in advance!

  • #2
    here is one way:
    Code:
    egen newvar=group(buyerID sellerID)
    you may want the label (sub)option - read the help file
    you can then just
    Code:
    ta newvar

    Comment


    • #3
      Maarten:
      you may want to try something along the following lines:
      Code:
      set obs 3
      g Buyer=1
      g Seller=_n+1 in 1/2
      replace Seller=3 in 3
      egen intersection=group(Buyer Seller)
      bysort Buyer Seller: count if intersection>=1
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Originally posted by Carlo Lazzaro View Post
        Maarten:
        you may want to try something along the following lines:
        Code:
        set obs 3
        g Buyer=1
        g Seller=_n+1 in 1/2
        replace Seller=3 in 3
        egen intersection=group(Buyer Seller)
        bysort Buyer Seller: count if intersection>=1
        Last edited by Carlo Lazzaro; 04 Jul 2016, 06:46. Reason: PS: I beg your pardon Inadvertent self-quotation.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Sounds like

          Code:
           
          egen group = group(buyer seller) , label
          except that "do business" might mean also same partners, reversed roles. If so, see http://www.stata-journal.com/sjpdf.h...iclenum=dm0043

          Comment


          • #6
            Thanks everybody for the valuable advice!

            Comment


            • #7
              I have an additional question. With the answers provided, the codes count the number of combinations. Additionally, I want to count the number of each combination. So, the combination '1 2' '1 2' should be counted as 2. I have enclosed an image of my dataset for more clarity.
              Attached Files

              Comment


              • #8
                Code:
                bysort group: gen freq = _N
                Consider also (destructive reductions)


                Code:
                contract group
                rename _freq freq 
                contract freq

                Comment


                • #9
                  Thanks for the solution!

                  Comment

                  Working...
                  X