I want to get average of L1-L4 without missing value and also get the score of what variable from L1-L4 they calculated from (L1=1, L2=2, L3=3, L4=4)? For example, ID1=. , ID2 average L1-L4 = (0.83+0.96+1.02)/3 and the score is 2+3+4 = 9 (because we did not use L1 as it is a missing value) or ID 3 average= (0.83+0.885+0.892_0.874)/4 with the score of 1+2+3+4 = 10 as we used every single data. ID8 average= (1.035+1.226+1.214)/3 with the score = 1+2+4 = 7 (L3 was missing)
input double(L1 L2 L3 L4)
. . . .
. .83 .961 1.022
.83 .885 .892 .874
.774 .779 .897 .967
1.222 . . 1.382
.775 .943 1.017 1.129
.979 1.087 1.041 1.046
1.035 1.226 . 1.214
I think I know the code to calculate average but how can I generate the score?
gen n_= !missing(L1)+ !missing(L2)+ !missing(L3)+ !missing(L4)
foreach i in L1 L2 L3 L4{
replace `i' =. if `i'==0
}
egen mean_L = rowmean(L1 L2 L3 L4)
input double(L1 L2 L3 L4)
. . . .
. .83 .961 1.022
.83 .885 .892 .874
.774 .779 .897 .967
1.222 . . 1.382
.775 .943 1.017 1.129
.979 1.087 1.041 1.046
1.035 1.226 . 1.214
I think I know the code to calculate average but how can I generate the score?
gen n_= !missing(L1)+ !missing(L2)+ !missing(L3)+ !missing(L4)
foreach i in L1 L2 L3 L4{
replace `i' =. if `i'==0
}
egen mean_L = rowmean(L1 L2 L3 L4)
Comment