Hi everyone,
I use id and type to generate dummy_1~dummy_5
My data looks like this:
But I want to have this:
As a result, my command is like this:
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:
I'm confusing about it now. Can anyone tell me how to do it?
Thanks for your help
Li
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

Comment