Skip the invalid part of the sample in a do loop

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
nybammer
Posts: 15
Joined: Thu May 22, 2014 5:18 am

Skip the invalid part of the sample in a do loop

Unread post by nybammer »

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:

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,
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Skip the invalid part of the sample in a do loop

Unread post by TomDoan »

This is the correct coding for computing the correlation for individual i and year matching j.

cmom(smpl=%indiv(t)==i.and.p_year==j,corr); # p_ret p_ret{1}

I'm assuming that you're getting problems with empty years. You can test for an empty range using an SSTATS first to count the number of valid entries:

Code: Select all

sstats(smpl=%indiv(t)==i.and.p_year==j) / %valid(p_ret).and.%valid(p_ret{1})>>count
if count>0 {
   cmom(smpl=%indiv(t)==i.and.p_year==j,corr); # p_ret p_ret{1}
   compute rho1_price2(i)=%cmom(2,1) 
}
else
  compute rho1_price2(i)=%na


I would note, however, that your "bookkeeping" with the rho's isn't going to get you what you want, since it keeps overwriting rho1_price(i) with each year's value. You would probably be better off with

dec rect rho1_price(nindiv,20)

and then use

compute rho1_price(i,j-1994)=....
Post Reply