Announcement

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

  • Collapse across multiple variables combinations

    Hello,

    I have a dataset that looks something like this

    Var1 Var2 Var3 Var4
    . . . .
    1 . . .
    1 1 . .
    . . . 1
    1 . 1 1
    1 1 . .
    1 . 1 1


    I would l like to be able to calculate the total across all of the combinations of the 4 variables so that i end up with:

    Var1 Var2 Var3 Var4 Total
    . . . . 1
    1 . . . 1
    1 1 . . 2
    . . . 1 1
    1 . 1 1 2



    I have managed to find a long-winded way to do this where I generate a variable to represent each of the combinations and then collapse this variable similar to below


    Code:
    * Generate combinations *
    gen tot1 = .
    replace tot1 = 1 if Var1 == . & Var2 == . & Var3 == 1 & Var4 == .
    
    gen tot2 = .
    replace tot2 = 1 if Var1 == 1 & Var2 == . & Var3 == 1 & Var4 == .
    ....
    
    gen tot5 = .
    replace tot5 = 1 if Var1 == 1 & Var2 ==. & Var3 == 1 & Var4 == 1
    
    * Collapse for totals *
    collapse (sum) tot1 - tot5
    
    gen N = 1
    reshape long tot, i(N) j(Variable)
    rename tot Total
    drop N
    
    label define var 1 "No Variables" 2 "Var1" 3 "Var1 & Var2" 4 "Var4" 5 "Var1 & Var3 & Var4"
    label val Variable var
    But, as there are many more than 4 variables and a lot more combinations this method is very long-winded and it doesn't quite give me the form I would like.

    Any help is greatly appreciated!

    Best wishes,
    Cydney

  • #2
    Code:
    help contract 

    Comment


    • #3
      I wrote the original of contract and so can say something on why it was thought up,

      In principle you don't need it as

      Code:
      bysort Var1 Var2 Var3 Var4 : gen freq = _N 
      
      collapse freq, by(Var1 Var2 Var3 Var4)
      gets you to the same place. But that is a trick that is easy to miss. The original used an invented name to follow StataCorp advice against using standard English words (advice I have ignored elsewhere) but once it was adopted as part of official Stata (in Stata 6) it seemed clear that as an opposite of expand it deserved the name contract.

      Comment


      • #4
        Brilliant thank you very much for the help Nick!

        Comment

        Working...
        X