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
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
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
Any help is greatly appreciated!
Best wishes,
Cydney
Comment