Consider the following data
input gvkey ROA
1004 .053175
1004 .0573483
1004 .0474536
1004 .0264029
1004 -.0829894
1004 -.018074
1004 .0049401
1004 .021104
1004 .0359239
1004 .054944
1004 .0551714
1004 .0570965
1004 .0297313
1004 .0409843
1004 .0308441
1004 .0257382
1010 .0680225
1010 .0206306
1010 .0216928
1010 .0210568
1010 .0391341
1010 .021526
1010 .0729496
1013 .1162412
1013 .112816
1013 .0523967
1013 .2186375
1013 -.5151418
1013 -1.000699
1013 .0114838
1013 .0721173
1013 .040772
1013 .0602335
1013 -.0218116
1013 -.3530068
1013 .0420482
1019 .0426432
1019 .0562442
1019 .0349
1019 .0615266
1019 .0418342
1021 .075502
1021 -.1783399
1021 -.1578007
1021 -.0696072
1021 -.2012739
1021 .0107006
1021 -.2506618
1021 .2153723
1021 .233043
1021 .0700788
1021 -.1719848
1021 -.5162843
end
I am trying to find standard deviation with the following code, but it is not working. Can someone debug it. Thanks
input gvkey ROA
1004 .053175
1004 .0573483
1004 .0474536
1004 .0264029
1004 -.0829894
1004 -.018074
1004 .0049401
1004 .021104
1004 .0359239
1004 .054944
1004 .0551714
1004 .0570965
1004 .0297313
1004 .0409843
1004 .0308441
1004 .0257382
1010 .0680225
1010 .0206306
1010 .0216928
1010 .0210568
1010 .0391341
1010 .021526
1010 .0729496
1013 .1162412
1013 .112816
1013 .0523967
1013 .2186375
1013 -.5151418
1013 -1.000699
1013 .0114838
1013 .0721173
1013 .040772
1013 .0602335
1013 -.0218116
1013 -.3530068
1013 .0420482
1019 .0426432
1019 .0562442
1019 .0349
1019 .0615266
1019 .0418342
1021 .075502
1021 -.1783399
1021 -.1578007
1021 -.0696072
1021 -.2012739
1021 .0107006
1021 -.2506618
1021 .2153723
1021 .233043
1021 .0700788
1021 -.1719848
1021 -.5162843
end
I am trying to find standard deviation with the following code, but it is not working. Can someone debug it. Thanks
Code:
gen SD=. mata mata clear st_view(SD=., ., "SD") st_view(id=., ., " gvkey ") st_view(roa=., ., " ROA ") p=panelsetup(id,1) for (i=1; i<=rows(p); i++) { for (o=p[i,1]; o<=p[i,2]; o++) { h = J(1,1,.) h = h \ roa[o,1] SD[o,1]=sqrt(variance(h[(2.. rows(h)),.])) } } end
Comment