I am trying to find out which variable produces the highest correlation coefficient with var1 within subjects.

I have 10 variables (var1-var10) for 10 days (day) for 10 individuals (id) in long format. However, I recreated an example with 3 variables, for 5 days for 3 individuals in dataex:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte(id day) float(var1 var2 var3)
1 1   .7806329   .2003541   .9929246
1 2   .8852594   .7479552   .3049926
1 3    .573707  .22255337   .8362682
1 4   .2431343   .9553812   .1578373
1 5 .008077622   .8259419   .8112878
2 1   .4933739   .1188735  .36237845
2 2   .7138895  .55584866    .884728
2 3   .4031537   .3037445 .008933238
2 4   .1250264  .53452736   .7117536
2 5   .7928984   .3798434   .6441231
3 1  .25131217 .063004404   .9543635
3 2   .6858981   .6998435   .8498245
3 3   .9965313    .483217   .4657771
3 4   .9135284   .8579231  .13088849
3 5  .29056227  .25628337   .5243536
end
I want to find out, for each id, which of variables var2-var10 has the highest Pearson's correlation with var1.

The best I can do is:

Code:
local varlist "var2 var3"

foreach i in 1 2 3 {
    display _newline
    foreach a in `varlist' {
        qui pwcorr var1 `a' if id==`i'
        local `i'_`a'_r = r(rho)
        if r(rho) !=. & r(rho)!=0 { //in the actual dataset, var1 may not be available or may not vary. This is how I exclude them from the list. 
            display "`i'_`a'_r:" ``i'_`a'_r'
        }
    }    
}
which outputs:

Code:
1_var2_r:-.54861605
1_var3_r:.04435157


2_var2_r:-.05894353
2_var3_r:.25521847


3_var2_r:.79714705
3_var3_r:-.61136201
The next step is to sort these by absolute correlation coefficient within id so that I know which of var2-var3 correlates highest with var1 within each id. For this exercise, I do not care for the significance.

Just to remind you that the example data is much smaller (3 x 5 x 3) than the actual data (10 x 10 x 10), so in the latter case it makes more sense to sort the correlation coefficients.

Thanks so much.