I use id and type to generate dummy_1~dummy_5
My data looks like this:
id | type | dummy_1 | dummy_2 | dummy_3 | dummy_4 | dummy_5 |
1 | 2 | 0 | 1 | 0 | 0 | 0 |
1 | 4 | 0 | 0 | 0 | 1 | 0 |
2 | 1 | 1 | 0 | 0 | 0 | 0 |
2 | 3 | 0 | 0 | 1 | 0 | 0 |
2 | 5 | 0 | 0 | 0 | 0 | 1 |
3 | 2 | 0 | 1 | 0 | 0 | 0 |
4 | 5 | 0 | 0 | 0 | 0 | 1 |
id | dummy_1 | dummy_2 | dummy_3 | dummy_4 | dummy_5 |
1 | 0 | 1 | 0 | 1 | 0 |
2 | 1 | 0 | 1 | 0 | 1 |
3 | 0 | 1 | 0 | 0 | 0 |
4 | 0 | 0 | 0 | 0 | 1 |
forvalues i=1(1)5{
bysort id: replace dummy_`i'=sum(dummy_`i'),
}
I thought I can use this command first, and delete the same data by using "duplicates drop id type, force" to get what I wanted.
However, after use the command:
forvalues i=1(1)5{
bysort id: replace dummy_`i'=sum(dummy_`i'),
}
I get this:
id | type | dummy_1 | dummy_2 | dummy_3 | dummy_4 | dummy_5 |
1 | 2 | 0 | 1 | 0 | 0 | 0 |
1 | 4 | 0 | 1 | 0 | 1 | 0 |
2 | 1 | 1 | 0 | 0 | 0 | 0 |
2 | 3 | 1 | 0 | 1 | 0 | 0 |
2 | 5 | 1 | 0 | 1 | 0 | 1 |
3 | 2 | 0 | 1 | 0 | 0 | 0 |
4 | 5 | 0 | 0 | 0 | 0 | 1 |
Thanks for your help
Li
0 Response to How to get sum of category
Post a Comment