Skip the invalid part of the sample in a do loop
Posted: Tue Jul 14, 2015 6:00 am
Dear Tom,
I am trying to compute the (annual) first order autocorrelation based on daily data for each stock in the SP500. Some stocks do not trade for all the years in the sample. Thus, the do loop stops once it encounters such a case. I have tried to use the %valid() to skip those years for those stocks, but it does not seem to work as intended. I am attaching below the code and the data:
Here is the data:
does not work (I guess since %valid() generates 1's and 0's). Do you have a suggestion on how to approach this issue?
Many thanks,
I am trying to compute the (annual) first order autocorrelation based on daily data for each stock in the SP500. Some stocks do not trade for all the years in the sample. Thus, the do loop stops once it encounters such a case. I have tried to use the %valid() to skip those years for those stocks, but it does not seem to work as intended. I am attaching below the code and the data:
Code: Select all
open data data_wrds1_upd2.dta
data(format=dta) / permno date year sic1 sic2 price Val_Weight_Ret_incl_div Val_Weight_Ret_excl_div $
Eq_Weight_Ret_incl_div Eq_Weight_Ret_excl_div S_P_500_Ret
pform(indiv=permno,time=date) p_permno
# permno
pform(indiv=permno,time=date) p_date
# date
pform(indiv=permno,time=date) p_year
# year
pform(indiv=permno,time=date) p_sic1
# sic1
pform(indiv=permno,time=date) p_sic2
# sic2
pform(indiv=permno,time=date) p_price
# price
pform(indiv=permno,time=date) p_vwrid
# Val_Weight_Ret_incl_div
pform(indiv=permno,time=date) p_vwred
# Val_Weight_Ret_excl_div
pform(indiv=permno,time=date) p_ewrid
# Eq_Weight_Ret_incl_div
pform(indiv=permno,time=date) p_ewred
# Eq_Weight_Ret_excl_div
pform(indiv=permno,time=date) p_sp500
# S_P_500_Ret
cal(panel=%nobs)
all %ngroup//%panelobs()
set p_ret = log(p_price) - log(p_price{1})
dec vect rho1_price2(nindiv*20)
dofor j = 1995 to 2014
do i=1,nindiv
cmom(smpl=%indiv(t)==i.and.p_year==j,corr); # p_ret p_ret{1}
compute rho1_price2(i)=%cmom(2,1)
end do i
end dofor j
dis rho1_price2
Here is the data:
For instance, using
Code: Select all
...
cmom(smpl=%valid(%indiv(t)==i).and.p_year==%valid(j),corr); # p_ret p_ret{1}
...
does not work (I guess since %valid() generates 1's and 0's). Do you have a suggestion on how to approach this issue?
Many thanks,