Hello Statalist colleagues,

I hope you are all staying healthy.

I had a question while struggling with the data to create three series called Depth1 and Depth2 and Breadth. I was thinking of making this manually as I got stuck in doing this, but wanted to reach out to statalist before that.
I thought this could be somehow done with bysort egen count and this sort of codes. But failed.. It would be great if I could get some ideas from more proficient fellows.

My dataset looks like the following below.

What I want to create are:

Depth1: In each set of year and panelkey, I'd like to show the count of the countrycode that appears the most. For example, in 2006 for panelkey 102634, 123 appears the most and it appears 3 times. So I want to assign 3 to all these observations.
Depth2: In each set of year and panelkey, I'd like to show sum(count of different country code)/ number of different country code in a year and panelkey. For example, in 2009 for panel 5152235, 107 appears 7 times and 128 appears 2 times. And the number of different countrycode is 2. So I want to have (7+2)/2 in the Depth2 series for this observation.
Breadth: In addition, it would be great if I could also know how I can generate a series for counting different number of country codes within a year and panelkey set. So for 2006 and panelkey 102634, there are two different countries 123 and 128. So I'd like to have 2 in this observation. This is also the denominator in Depth2 calculation.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
year panelkey countrycode Depth1 Depth2 Breadth
2006  102634 123 3 (3+1)/2 2
2006  102634 123 3 (3+1)/2 2
2006  102634 123 3 (3+1)/2 2
2006  102634 128 3 (3+1)/2 2
2008  102634 123 2 (2+1)/2 2
2008  102634 123 2 (2+1)/2 2
2008  102634 128 2 (2+1)/2 2
.
.
.
2017  102634 123 1 (1+1)/2 2
2017  102634 128 1 (1+1)/2 2
2018  102634 123 1 (1+1)/2 2
2018  102634 128 1 (1+1)/2 2
2006 1122135 301 1 1/1 1
2007 1122135 301 1 1/1 1
2008 1122135 301 1 1/1 1
2009 1122135 301 1 1/1 1
.
.

2009 5152235 107 7 (7+2)/2 2
2009 5152235 107 7 (7+2)/2 2
2009 5152235 107 7 (7+2)/2 2
2009 5152235 107 7 (7+2)/2 2
2009 5152235 107 7 (7+2)/2 2
2009 5152235 107 7 (7+2)/2 2
2009 5152235 107 7 (7+2)/2 2
2009 5152235 128 7 (7+2)/2 2
2009 5152235 128 7 (7+2)/2 2
.
.
.
end
Thank you so much for your information.

Thanks!