Hi !
I have been using STATA not for so long. And now I'm trying to loop over 2 variables at the same time in order to generate a new variable. My data, as shown in the attached dta file looks like lpermno trt1m (which is the monthly stock returns) year mydatemonthly rf.
The question is that, for every lpermno, I want to generate monthly excess returns=trt1m/100-rf.
I tried doing the following but it's not working:
egen group = group( lpermno mydatemonthly)
su group, meanonly
foreach i of num 1/`r(max)' {
gen ret= trt1m- rf if group == `i'
} //invalid numlist has too many elements
Then, I tried this but in vain:
egen group1 = group(mydatemonthly)
su group1, meanonly
local maxrf = r(max)
egen group2 = group( lpermno)
su group2, meanonly
local maxlpermno = r(max)
foreach i of num 1/`maxlpermno' {
foreach t of num 1/`maxrf' {
gen ret= trt1m/100- rf if group1 == `i' & group2 == `t'
}
}
Any suggestions are welcome !
I have been using STATA not for so long. And now I'm trying to loop over 2 variables at the same time in order to generate a new variable. My data, as shown in the attached dta file looks like lpermno trt1m (which is the monthly stock returns) year mydatemonthly rf.
The question is that, for every lpermno, I want to generate monthly excess returns=trt1m/100-rf.
I tried doing the following but it's not working:
egen group = group( lpermno mydatemonthly)
su group, meanonly
foreach i of num 1/`r(max)' {
gen ret= trt1m- rf if group == `i'
} //invalid numlist has too many elements
Then, I tried this but in vain:
egen group1 = group(mydatemonthly)
su group1, meanonly
local maxrf = r(max)
egen group2 = group( lpermno)
su group2, meanonly
local maxlpermno = r(max)
foreach i of num 1/`maxlpermno' {
foreach t of num 1/`maxrf' {
gen ret= trt1m/100- rf if group1 == `i' & group2 == `t'
}
}
Any suggestions are welcome !
Comment