Below is the data example, I want to generate average of monthly correlations. I run the code that follows this data example but confront the below error after the execution of loop function in the code. Need to debug the issue.
variable mdate already defined
r(110);
variable mdate already defined
r(110);
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(date mdate smb hml) 21003 690 .004645994 -.005159753 21004 690 .004073281 .002022839 21005 690 .007332436 -.005572066 21006 690 .006365304 -.000181598 21007 690 .006878641 -.004705894 21010 690 -.009105763 -.00480592 21011 690 .001163057 .008666458 21012 690 -.000817509 -.006586943 21013 690 -.000666241 -.002847255 21014 690 -.005759656 -.000659944 21017 690 -.003070122 -.004755242 21018 690 .000274434 .0000683381 21019 690 .005044285 -.002300632 21020 690 .004944477 -.001597104 21021 690 -.002151537 .00288309 21024 690 -.002220035 -.002052638 21025 690 -.004069697 -.005320283 21026 690 -.001728626 -.003543576 21027 690 -.007841507 .007511681 21028 690 .0000234654 -.000936668 21031 690 -.006997531 -.007221039 21032 691 -.006583289 .00019656 21033 691 -.00148398 .0000951234 21034 691 -.005790243 .007931685 21035 691 -.00986818 -.007765353 21038 691 .002256937 -.006828004 21039 691 -.009300191 .002929728 21040 691 -.008887877 .000939124 21041 691 -.01904482 .010826387 21042 691 .007058075 .007043888 21045 691 .009646596 -.008371387 21047 691 .001621883 -.001985831 21048 691 .005605833 -.008747665 21049 691 -.003859418 .000962241 21052 691 .00437177 .005338254 21053 691 -.006844101 .002244503 21054 691 -.001270297 -.007827954 21055 691 .004983399 -.002500948 21059 691 .003391818 -.002881391 21060 691 .001262542 .003673771 21061 691 .006223509 -.004668508 21062 691 .008711083 .002252869 21063 692 .005372665 -.003645938 21066 692 .003697511 .002967378 21067 692 .009585852 -.002523657 21068 692 .009511882 -.005653809 21069 692 .007188244 -.003333259 21070 692 -.001239172 .000990439 21073 692 -.000659754 .000634092 21074 692 -.004681574 -.001886672 21075 692 -.002377506 .003232573 21076 692 .003256121 .001905745 21077 692 -.000776011 .004644539 21080 692 .003976348 -.002874948 21081 692 .007676908 -4.28e-06 21082 692 .005287422 -.004754693 21083 692 .005788573 .004476291 21084 692 -.006634001 .009705726 21087 692 -.011319405 .002122743 21088 692 .005336226 -.004654214 21089 692 -.006859979 .006557258 21090 692 .004472815 -.001095178 21091 692 .000949041 -.002499559 21095 693 -.002522548 -.000639198 21096 693 .003451545 -.000338546 21097 693 .00966074 -.003989066 21098 693 .003760163 -.008360161 21101 693 .015382366 .00285262 21102 693 .009961512 -.000860564 21103 693 -.006358726 .005480116 21104 693 .004995958 -.000938155 21105 693 -.010228984 -.007542937 21108 693 -.00702354 -.006090971 21109 693 -.000408795 -.005765915 21110 693 .007927561 .000519172 21111 693 .008725218 -.004472313 21115 693 -.003704251 -.008556928 21116 693 .004811698 -.003649875 21117 693 -.034631077 -.03765541 21118 693 -.002191295 -.007144463 21119 693 .008728828 .009158449 21122 693 .00634425 -.006260186 21123 693 .005072616 .002042857 21124 694 -.005401004 -.009626897 21125 694 .005562566 .00209888 21126 694 .006038947 .001381735 21129 694 .007630029 .006700573 21130 694 -.000271678 .01433504 21131 694 .003133615 .013299184 21132 694 .00224903 -.005719649 21133 694 -.007506684 .000360494 21136 694 .003784826 .001413408 21137 694 .001917472 .000321209 21138 694 -.004075451 .012619417 21139 694 .003217167 -.004356314 21140 694 -.001650682 -.003375155 21143 694 .00869631 -.006093229 21144 694 .007867894 -.003281062 21145 694 .00790867 .001719831 21146 694 .009219537 -.001196315 end format %td date format %tm mdate
Code:
local smb hml
local vbles: list sort vbles
frame create correlations
frame create cumulative
levelsof mdate, local(mdates)
foreach r of local mdates {
capture corr `vbles' if mdate == `r'
if c(rc) == 0 {
matrix C = r(C)
frame correlations {
svmat C, names(col)
gen mdate = `r'
gen vble = ""
local i = 1
foreach v of local vbles {
replace vble = `"`v'"' in `i'
local ++i
}
}
frame cumulative: frameappend correlations
frame correlations: clear
}
else if c(rc) != 2000 { // UNEXPECTED ERROR
display as error "Unexpected error in -corr-"
exit `c(rc)'
}
else {
continue
}
}
frame drop correlations
frame change cumulative
frame put _all, into(averages)
frame change averages
collapse (mean) `vbles', by(vble)

Comment