        PROGRAM mkind
	INCLUDE '/wrds/crsp/lib/acclib/include/us.txt'
C	INCLUDE 'us.txt'
C	include 'usdec.txt'
	parameter (maxport=26, maxlen=9500)
	Integer	  portofsic(99), numport, ivw, iday1, iday2, jday1, jday2
	Integer	  sicnow, numstk(maxlen,maxport), maxpermno, idaily
	Real*8	  pret(maxlen,maxport), value(maxlen,maxport)
	Real*8	  lvalue(maxlen,maxport), dolval
C
	Open(10,file='mkind.inp',STATUS='old')
	 read(10,*) iday1 
	 read(10,*) iday2
	 read(10,*) ivw
	 read(10,*) maxpermno
	 read(10,*) idaily
	close(10)
	if (idaily.eq.1) then
		call udopen(3)
	else
		call umopen(3)
	endif
  	jday1 = indxdt(iday1)
	jday2 = indxdt(iday2) 
	if (jday2-jday1+1 .gt. maxlen) then
  	write(*,*) iday1,iday2
	 write(*,*) 'Too many dates, increase maxlen and recompile'
	 goto 11
	endif
C
	Open(10,file='dm03.out',STATUS='old')
	read(10,*) numport
	do i = 1 , 99
	  read(10,*) itrash, portofsic(i)
	enddo
	close(10)
C
	do k = 1 , numport
	do it = 1 , jday2-jday1+1  
	  value(it,k)  = 0d0
	  lvalue(it,k) = 0d0
	  pret(it,k)   = 0d0
	  numstk(it,k) = 0
	enddo
	enddo
C
C	call udcal	
	Call usgetper(1,10000,infos+returns+prices,*900)	
C	do it = jday1 , jday2
C	  write(11,100) caldt(it),decret(1,it),decret(5,it),decret(10,it)
C	Enddo
	if (iskip.eq.1) goto 11
	Do while (permno .le. maxpermno)
	  it = 0
	  Do i1 = jday1 , jday2		! date loop for every security
	    it = it + 1
	    if (i1 .lt. indxdt(caldt(begdat))) goto 7
	    if (i1 .gt. indxdt(caldt(enddat))) goto 7
 	    if (ret(i1) .lt. -1d0) go to 7 
   	    if (names(shrcd,curnam(caldt(i1)))  .ge. 22) go to 7
  	    if (names(shrcd,curnam(caldt(i1)))  .eq. 12) go to 7
    	    if (names(exchcd,curnam(caldt(i1))) .lt.  1) go to 7
	    sicnow = names(siccd,curnam(caldt(i1)))/100
	    if (sicnow .eq. 0) sicnow = hsiccd/100
	    if ((sicnow .eq. 0) .or. (sicnow .gt. 99)) goto 7
	    k = portofsic(sicnow)
	    if (k .eq. 0) goto 7	!This SIC not allocated
	    if (ivw .eq. 1) then 			! Value weight
		value(it,k)  = value(it,k)  + curshr(i1)*abs(prc(i1)) 
		dolval       = curshr(i1-1)*abs(prc(i1-1))
		lvalue(it,k) = lvalue(it,k) + dolval
		pret(it,k)   = pret(it,k) + dolval*ret(i1) 
	      else
		pret(it,k) = pret(it,k) + ret(i1)	! Equal weight
	    endif
     	    numstk(it,k) = numstk(it,k) + 1 
C	     if (i1.eq.jday2) write(11,*) permno,k
7	  Enddo
	  Call usgetper(1,inext,infos+returns+prices,*900)
	Enddo
900	if (idaily .eq. 1) then
		call udclose
	else
		call umclose
	endif
C
C      For value weights only
C
	if (ivw .eq. 1) then
	 do k = 1 , numport
	 it = 0
       	 do i1 = jday1,jday2
	  it = it + 1 
	  if ((lvalue(it,k).gt.0) .and. (value(it,k).gt.0)) then  
		pret(it,k) = pret(it,k) / lvalue(it,k)
	  else 
	   pret(it,k) = -99 
	  endif 
	 enddo   	
	 enddo
	endif
C
C	Output portfolio returns
C
	Open(12,file='dm03ret.dat',status='unknown')
	Open(13,file='dm03num.dat',status='unknown')
	Open(14,file='dm03mva.dat',status='unknown')
	if (ivw .eq. 1) then
	 do  i = 1 , jday2-jday1+1
	  write (12,100) caldt(i+jday1-1),(pret(i,j),j=1,numport)
       	  write (13,101) caldt(i+jday1-1),(numstk(i,j),j=1,numport)
       	  write (14,100) caldt(i+jday1-1),(value(i,j),j=1,numport)
       	 enddo
	else
	 do  i = 1 , jday2-jday1+1
	  write (12,100) caldt(i+jday1-1),((pret(i,j)/dfloat(numstk(i,j))),j=1,numport)
       	  write (13,101) caldt(i+jday1-1),(numstk(i,j),j=1,numport)
       	  write (14,100) caldt(i+jday1-1),(value(i,j),j=1,numport)
	 enddo
	endif
100	format(1x,i8,50(1x,e10.4))
101	format(1x,i8,50(1x,i4))
	Close(12)
	Close(13)
	Close(14)
11	End

