!    program DOCCEX.FOR
 
!     reads inc22, con47, and oth40; creates the file cexucc, used
!     to properly allocate income and spending (ucc) 
!     categories in the cex to the 109 consumer unit record categories.
!     also creates cextitle, containing 109 category titles

Subroutine doccex
      
      integer cat(109),ucc(800),hucc(800),catcnt,ucccnt
      integer ucccat(800),hucccat(800),incode1,incode2
      character*1 sign,uccsign(800),type,ucctype(800),annual, testch
      character*1 uccann(800),huccann(800)
      character*1 huccsign(800),hucctype(800)
      character*60 title(109)

      data title/109*' '/
      data uccsign/800*' '/
      data ucctype/800*' '/
      data huccsign/800*' '/
      data hucctype/800*' '/
      data huccann/800*' '/
      data uccann/800*' '/
      data ucc/800*0./
      data hucc/800*0./
      data cat/109*0./
      data hucccat/800*0./
      data ucccat/800*0./

      open(unit=12,file='I:\cexnber\files needed for input processing\inc22')
      open(unit=13,file='I:\cexnber\files needed for input processing\con47')
      open(unit=14,file='I:\cexnber\files needed for input processing\oth40')
      open(unit=15,file='I:\cexnber\files needed for input processing\cextitle')
      open(unit=16,file='I:\cexnber\files needed for input processing\cexucc')
      
      ucccnt=0.
      catcnt=1.

  105 read(12,'(i3,2x,a60)',end=111) cat(catcnt),title(catcnt)
      write(15,'(i3,1x,a60)') catcnt,title(catcnt)
      catcnt=catcnt+1.
  110 continue
      sign=' '
      type=' '
      annual=' '
      incode1=0.
      incode2=0.
      testch=' '
      read(12,'(bz,a1,a1,i3,i3,a1,a1)',end=111) sign,type,incode1,incode2,annual,testch
      if ((type.eq.' ').and.(testch.eq.' ')) goto 105
      if ((type.eq.' ').and.(testch.ne.' ')) goto 110
      ucccnt=ucccnt+1
      write(77,*) ucccnt,incode1,incode2
      ucccat(ucccnt)=cat(catcnt-1)
      uccsign(ucccnt)=sign
      ucctype(ucccnt)=type
      uccann(ucccnt)=annual
      ucc(ucccnt)=(1000*incode1)+incode2
      goto 110
  111 continue

  115 read(13,'(i3,2x,a60)',end=121) cat(catcnt),title(catcnt)
      cat(catcnt)=cat(catcnt)+22
      write(15,'(i3,1x,a60)') catcnt,title(catcnt)
      catcnt=catcnt+1.
  120 continue
      sign=' '
      type=' '
      annual=' '
      incode1=0.
      incode2=0.
      testch=' '
      read(13,'(bz,a1,a1,i3,i3,a1,a1)',end=121) sign,type,incode1,incode2,annual,testch
     
      if ((type.eq.' ').and.(testch.eq.' ')) goto 115
      if ((type.eq.' ').and.(testch.ne.' ')) goto 120
      ucccnt=ucccnt+1
      write(77,*) ucccnt,incode1,incode2
      ucccat(ucccnt)=cat(catcnt-1)
      uccsign(ucccnt)=sign
      ucctype(ucccnt)=type
      uccann(ucccnt)=annual
      ucc(ucccnt)=(1000*incode1)+incode2
      goto 120
  121 continue

  125 read(14,'(i3,2x,a60)',end=131) cat(catcnt),title(catcnt)
      cat(catcnt)=cat(catcnt)+69
      write(15,'(i3,1x,a60)') catcnt,title(catcnt)
      catcnt=catcnt+1.
  130 continue
      sign=' '
      type=' '
      annual=' '
      incode1=0.
      incode2=0.
      testch=' '
      read(14,'(bz,a1,a1,i3,i3,a1,a1)',end=131) sign,type,incode1,incode2,annual,testch
      if ((type.eq.' ').and.(testch.eq.' ')) goto 125
      if ((type.eq.' ').and.(testch.ne.' ')) goto 130
      ucccnt=ucccnt+1
      write(77,*) ucccnt,incode1,incode2
      ucccat(ucccnt)=cat(catcnt-1)
      uccsign(ucccnt)=sign
      ucctype(ucccnt)=type
      uccann(ucccnt)=annual
      ucc(ucccnt)=(1000*incode1)+incode2
      goto 130
  131 continue


  300 do 400 j=1,ucccnt
       do 350 n=1,ucccnt-j
       if (ucc(j).gt.ucc(j+n)) then
        hucc(j)=ucc(j+n)
        hucccat(j)=ucccat(j+n)
        huccsign(j)=uccsign(j+n)
        hucctype(j)=ucctype(j+n)
        huccann(j)=uccann(j+n)
        ucc(j+n)=ucc(j)
        ucccat(j+n)=ucccat(j)
        uccsign(j+n)=uccsign(j)
        ucctype(j+n)=ucctype(j)
        uccann(j+n)=uccann(j)
        ucc(j)=hucc(j)
        ucccat(j)=hucccat(j)
        uccsign(j)=huccsign(j)
        ucctype(j)=hucctype(j)
        uccann(j)=huccann(j)
        hucc(j)=0
        hucccat(j)=0
        huccsign(j)=' '
        hucctype(j)=' '
        huccann(j)=' '
      end if
  350 continue
  400 continue
      

  700 do 800 i=1,ucccnt
      if(ucccat(i).ne.0) then
       if (ucc(i).ne.0) then
        write(16,'(i3,a1,a1,a1,i6)') ucccat(i),uccsign(i),   ucctype(i),uccann(i),ucc(i)
                         
       end if
      end if
  800 continue 
      write(77,*) ucccnt

close(12)
close(13)
close(14)
close(15)
close(16)

  999 end subroutine doccex
