I have two variables:
I want to make a new variable (cov_sum) that is the sum of any covenanttype in packageid.
* Example generated by -dataex-. To install: ssc install dataex
clear
input double packageid long CovenantType
4106 13
4106 18
4812 13
4952 2
5223 9
5287 18
7011 4
7011 13
7011 18
7849 17
10261 17
11057 4
11178 4
11178 13
11178 18
11452 17
11714 4
11714 13
11954 2
11954 13
11954 18
12807 13
12807 18
end
So for example, packageid 4106 would have cov_sum = 2 because it has two CovenantType values, 13 and 18.
I've created 21 temporary variables to be 1 or 0 for each type of covenant, but I'm not sure how to do this last step to sum them for each packageid.
I'm aware it would create repetitive values, like below but that is okay.
input double packageid long CovenantType long? cov_sum
4106 13 2
4106 18 2
How can I create a variable that is the sum of any covenanttype in packageid?
The following works for me:
bysort packageid: generate n = _n
bysort packageid: egen cov_sum = max(n)
list, abbreviate(15)
+----------------------------------------+
| packageid CovenantType n cov_sum |
|----------------------------------------|
1. | 4106 13 1 2 |
2. | 4106 18 2 2 |
3. | 4812 13 1 1 |
4. | 4952 2 1 1 |
5. | 5223 9 1 1 |
|----------------------------------------|
6. | 5287 18 1 1 |
7. | 7011 4 1 3 |
8. | 7011 13 2 3 |
9. | 7011 18 3 3 |
10. | 7849 17 1 1 |
|----------------------------------------|
11. | 10261 17 1 1 |
12. | 11057 4 1 1 |
13. | 11178 4 1 3 |
14. | 11178 13 2 3 |
15. | 11178 18 3 3 |
|----------------------------------------|
16. | 11452 17 1 1 |
17. | 11714 4 1 2 |
18. | 11714 13 2 2 |
19. | 11954 2 1 3 |
20. | 11954 13 2 3 |
|----------------------------------------|
21. | 11954 18 3 3 |
22. | 12807 13 1 2 |
23. | 12807 18 2 2 |
+----------------------------------------+
bysort packageid : gen cov_sum = _N
would seem to be more direct. (You know this really....)@NickCox, I wanted to show to the OP the steps.