I always get column vs. row percentages backwards, but I am trying to calculate row percentages (I think) and when I am collapsing (see code below) it gives me column percentages (I think).
For example, with this code:
I get this as my output:
So this reads as: "of those who attended, 8% were white, 44% were black, and 40% were hispanic".
But what I really want is this (which I just made up):
So this reads as "5% of white individuals attended". Is there a way to achieve this with a modified version of the collapse code that I used above? Example data here below.
(Note: the numbers in the top table may be different than this example data, since this is a very large dataset with roughly 1M observations and we just have a very small slice of data below).
For example, with this code:
Code:
collapse (percent) perc = id2 (mean) white black hispanic male group1 group2 group3 (count) id2 if !missing(attend), by(attend)
attend | perc | white | black | hispanic | male | group1 | group2 | group3 |
0 | 92.98 | .13 | .34 | .457 | .501 | .38 | .316 | .297 |
1 | 7.016 | .081 | .44 | .402 | .50 | .190 | .304 | .50 |
But what I really want is this (which I just made up):
attend | perc | white | black | hispanic | male | group1 | group2 | group3 |
0 | 92 | 95 | 90 | 94 | 50 | 88 | 90 | 96 |
1 | 8 | 5 | 10 | 6 | 50 | 12 | 10 | 4 |
So this reads as "5% of white individuals attended". Is there a way to achieve this with a modified version of the collapse code that I used above? Example data here below.
(Note: the numbers in the top table may be different than this example data, since this is a very large dataset with roughly 1M observations and we just have a very small slice of data below).
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(attend black hispanic white male group1 group2 group3 id2) 1 1 0 0 0 0 0 1 3498678 0 1 0 0 1 0 1 0 498298 0 1 0 0 0 0 1 0 598558 1 1 0 0 1 0 0 1 498568 0 0 0 0 0 0 0 1 908788 0 1 0 0 1 0 0 1 928798 1 1 0 0 0 0 1 0 498948 1 1 0 0 0 0 0 1 499108 0 1 0 0 1 0 0 1 1007078 0 1 0 0 0 0 0 1 1007148 0 1 0 0 1 0 1 0 999988 0 0 0 1 0 0 0 1 999398 0 1 0 0 0 0 1 0 1007778 0 1 0 0 0 0 1 0 1007878 1 1 0 0 1 0 1 0 1010178 0 1 0 0 0 0 0 1 1000248 0 0 0 1 0 0 1 0 999758 0 0 1 0 0 0 1 0 1008358 0 1 0 0 0 0 1 0 1000668 0 1 0 0 0 0 0 1 1008418 0 0 0 1 0 0 0 1 1010718 0 0 0 1 1 0 1 0 1010928 0 0 0 1 0 0 1 0 1001658 0 0 1 0 0 0 0 1 1011018 0 1 0 0 1 0 1 0 1003168 0 1 0 0 0 0 0 1 1008968 1 1 0 0 0 0 0 1 103528 0 1 0 0 1 0 0 1 12088 0 1 0 0 0 0 1 0 111398 1 1 0 0 1 0 1 0 1017768 0 1 0 0 1 0 1 0 1011468 0 1 0 0 0 0 1 0 105538 0 0 0 0 0 0 0 1 118098 0 1 0 0 0 0 0 1 1018208 1 0 0 1 1 0 0 1 1005868 0 0 0 1 1 0 0 1 1013788 0 1 0 0 0 0 0 1 1022618 1 0 0 0 1 0 1 0 1013928 0 0 1 0 0 0 1 0 1005888 0 0 0 0 0 0 1 0 1002808 0 0 0 0 1 0 1 0 1014548 0 0 0 1 1 0 1 0 1014558 1 0 1 0 1 0 0 1 1014588 0 0 0 0 1 0 0 1 1014638 0 0 1 0 0 0 1 0 1006078 0 0 1 0 0 0 0 1 1018898 0 0 0 1 0 0 0 1 1015458 0 0 1 0 1 0 1 0 1070248 1 1 0 0 1 0 1 0 1019198 0 0 1 0 1 0 1 0 1019288 0 0 1 0 0 0 1 0 1019778 0 0 1 0 0 0 0 1 1019888 0 0 0 1 1 0 1 0 1015938 0 1 0 0 1 0 0 1 1020278 0 0 1 0 1 0 1 0 1016028 1 0 1 0 1 0 1 0 1016238 0 0 1 0 0 0 0 1 1045148 0 0 1 0 0 0 0 1 1105568 0 0 1 0 1 0 1 0 1105578 1 0 1 0 0 0 1 0 1074808 1 1 0 0 0 0 0 1 1048028 0 1 0 0 0 0 0 1 1033758 0 0 1 0 0 0 1 0 1037358 1 1 0 0 0 0 0 1 1098818 0 0 0 1 1 0 0 1 1045328 0 1 0 0 1 0 0 1 1105588 0 1 0 0 0 0 0 1 1035038 1 0 0 0 0 0 1 0 1030148 0 0 0 1 1 0 1 0 1076538 1 0 0 0 1 0 0 1 1030238 0 0 1 0 0 0 1 0 1031428 0 0 1 0 1 0 1 0 1035228 0 0 1 0 1 0 1 0 1098248 0 0 1 0 1 0 1 0 1035438 0 0 0 0 0 0 0 1 1086638 0 1 0 0 0 0 0 1 1064348 0 1 0 0 0 0 1 0 1053608 0 1 0 0 0 0 0 1 1078298 0 1 0 0 0 0 0 1 1048478 0 1 0 0 0 0 1 0 1051638 0 1 0 0 0 0 0 1 1062848 0 0 1 0 1 0 1 0 1101448 0 0 1 0 0 0 1 0 1027228 0 1 0 0 0 0 1 0 1066538 0 1 0 0 0 0 0 1 1025848 0 1 0 0 0 0 0 1 1024628 0 1 0 0 0 0 1 0 1026928 0 1 0 0 1 0 0 1 1026258 0 1 0 0 1 0 1 0 1025888 0 0 1 0 1 0 1 0 1024668 0 1 0 0 0 0 1 0 1076174 0 1 0 0 1 0 1 0 1040404 0 0 1 0 1 0 0 1 1017804 0 0 1 0 1 0 1 0 1040485 0 0 1 0 1 0 1 0 1061646 0 0 0 1 0 0 1 0 1089547 0 0 1 0 0 0 0 1 2052265 0 0 1 0 1 0 0 1 2034166 0 0 1 0 1 0 0 1 1054195 0 0 0 0 0 0 0 1 1048755 end
Comment