The following provides one solution to the problem. However, it seems unnecessarily complicated to me. I would appreciate if fellow Stata users could suggest a simplification.
The dataset is the following:
Where I count the frequencies of (positive) treatment per group. I add these frequencies (treat==1) as a new variable (which is equal within each communityID).
The resulting data NRPS.dta then contains the collapsed information
What is the best way of solving this problem without saving and merging data?
The dataset is the following:
| ID | communityID | treat |
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 0 |
| 4 | 1 | 1 |
| 5 | 2 | 0 |
| 6 | 2 | 1 |
preserve
contract communityID treat, zero nomiss
drop if treat==0 // IN ORDER TO HAVE m:1 MERGE
save treat.dta, replace
restore
merge m:1 communityID using treat.dta
contract communityID treat, zero nomiss
drop if treat==0 // IN ORDER TO HAVE m:1 MERGE
save treat.dta, replace
restore
merge m:1 communityID using treat.dta
| communityID | treat | _freq |
| 1 | 1 | 3 |
| 1 | 0 | 1 |
| 2 | 1 | 1 |
| 2 | 0 | 1 |

Comment