I want to compute the correlation of x between different stocks in each half year. There are 20+ half years and 2000+ stocks. (Table 1). Therefore, I reshape the data firstly yh by yh(Table 2) ,and compute the correlation of x (Table 3).

I want to ask two questions:
1.Is there any concise solutions?
2.How can i save the output of pwcorr and correlate into dta?
Thanks for your reply!

Table 1
stkcd tradedate yh x
1 16806 92 0.078212
1 16807 92 0.128066
1 16811 92 0.597885
1 16812 92 0.012668
1 16813 92 0.2803
... ... ... ...
2027 16806 92 0.507343
2027 16807 92 0.299147
2027 16811 92 0.369548
2027 16812 92 0.216952
2027 16813 92 0.525851
  ... ... ...
1 17147 93 0.143762
1 17148 93 0.694211
1 17149 93 0.529129
1 17150 93 0.516332
1 17156 93 0.042779
... ... ... ...
2027 17147 93 0.82723
2027 17148 93 0.973761
2027 17149 93 0.122705
2027 17150 93 0.184536
2027 17156 93 0.250978
  ... ... ...
1 17280 94 0.354138
1 17281 94 0.918449
1 17282 94 0.315837
1 17295 94 0.112138
1 17296 94 0.47002
... ... ... ...
2027 17280 94 0.731127
2027 17281 94 0.637081
2027 17282 94 0.404287
2027 17295 94 0.923884
2027 17296 94 0.53458
  ... ... ...
Table 2
tradedate x1 x2 ... x2027 yh
16806 0.078212 ... ... 0.507343 92
16807 0.128066 ... ... 0.299147 92
16811 0.597885 ... ... 0.369548 92
16812 0.012668 ... ... 0.216952 92
16813 0.2803 ... ... 0.525851 92
... ... ... ... ... ...
Table 3
x1 x2 ... x2027
x1 1 ... ...
x2 0.152024 ... ...
... ... ... ... ...
x2027 0.001852 ... ... 1