c sew.compustat.fortran.txt 9/27/96 doncram c Sample Fortran program which reads a 1994 Compustat data tape on gsb-unix c Contributed by Seth Weingram. c To use, modify the filenames and the calculation steps to suit your c own purposes. program cstat character*8332 in_data(8) character*8332 in_data1 character*70 bad_data(960) integer ii,jj,kk,k1,k2 integer dnum,rec,file,zlist,fyr(5),yeara(5),xrel,stk,dup,blank integer ucode(5),state,county,finc,source(5) integer num_rec,num_rec1,num_rec2,num_com,max_num_com character*6 cnum integer cic character*28 name character*8 smbl character*2 aftnt(35,5) character*19 blank1 character*10 ein character*30 blank2 character*2 aftnt1(35,5) real data(175,5),data1(175,5),odata(350,5) equivalence(in_data,bad_data(1)) c read computstat file open(unit=1,file='/d2/compustat/research/annual', & status='old',form='formatted',access='sequential',readonly) c output actual data to this file open(unit=2,name='/d1/sew/test.val',status='unknown', & form='formatted') c output test messages to this file open(unit=3,name='/d1/sew/bad1993.dat',status='unknown', & form='formatted') num_rec=0 num_com=0 c read through up to max_num_com companies max_num_com = 10000 do 1000 ii = 1, max_num_com num_com=num_com+1 c read through the 8 8332 character blocks for the current company c if I remember correctly there are two types of data blocks, and each c is repeated four times. Each repetition contains 5 yrs of data. c So the there is 20 years of data for each of the vars in the two types c of blocks. do 100 jj = 1,8 num_rec=num_rec+1 read(1,10,end=2000) in_data(jj) c write(6,10) in_data(jj) 10 format(a8332) 100 continue c separate out each block into header variables and data arrays c cycle through all four repetitions of five years of data. do 200 kk = 1,4 k1=kk k2=kk+4 decode(8332,20,in_data(k1),err=50) dnum,cnum,cic,rec, & file,zlist,name,smbl,fyr,yeara,xrel,stk,dup, & blank,ucode,aftnt,data decode(8332,30,in_data(k2),err=51) dnum,cnum,cic,rec, & file,state,county,finc,source,blank1,ein,blank2, & aftnt1,data1 c combine the data and data1 arrays, with 175 rows each, into an overall c data array with 350 rows, odata. do j = 1,175 do i = 1,5 odata(j,i) = data(j,i) odata(j+175,i) = data1(j,i) enddo enddo 20 format(i4, a6, i3, i1, 2i2, a28, a8, 10i2, i4,i1,i2, & i6,5i1,175a2, & 5(13f10.3,f8.3,f10.3,f8.3,5f10.3,3f8.3, & f10.3,f8.3,f10.6,f10.3,f8.3,3f10.3,f8.3,f10.3, & f8.3,2f10.3,2f8.3,6f10.3,f8.3,3f10.3,3f8.3, & f10.4,6f8.3,3f10.3,3f8.3,f10.3,f8.3,3f10.3, & f8.3,f10.3,2f8.3,f10.3,6f8.3,3f10.3,2f8.3, & f10.3,4f8.3,f10.3,4f8.3,4f10.3,8f8.3,2f10.3, & f8.3,7f10.3,f8.3,6f10.3,5f8.3,3f10.3,f8.3, & 5f10.3,f8.3,4f10.3,2f8.3,f10.3,3f8.3,f10.3, & 11f8.3,f10.3,6f8.3,f10.3,f8.3,f10.3,2f8.3, & f10.3)) 30 format(i4,a6,i3,i1,2i2,i3,6i2,a19,a10,a30, & 175a2, & 5(13f10.3,f8.3,f10.3,f8.3,5f10.3,3f8.3, & f10.3,f8.3,f10.6,f10.3,f8.3,3f10.3,f8.3,f10.3, & f8.3,2f10.3,2f8.3,6f10.3,f8.3,3f10.3,3f8.3, & f10.4,6f8.3,3f10.3,3f8.3,f10.3,f8.3,3f10.3, & f8.3,f10.3,2f8.3,f10.3,6f8.3,3f10.3,2f8.3, & f10.3,4f8.3,f10.3,4f8.3,4f10.3,8f8.3,2f10.3, & f8.3,7f10.3,f8.3,6f10.3,5f8.3,3f10.3,f8.3, & 5f10.3,f8.3,4f10.3,2f8.3,f10.3,3f8.3,f10.3, & 11f8.3,f10.3,6f8.3,f10.3,f8.3,f10.3,2f8.3, & f10.3)) c pick out the data required and data to be printed from each five year c segment. do i = 1,5 c this code was used to create certain preferred stock values for c each repetition (5 year periods). psval = -0.001 psval = odata(56,i) if (psval .eq. -0.001) psval = odata(10,i) if (psval .eq. -0.001) psval = odata(130,i) c print out requested data. write(2,40) cnum,cic,yeara(i),zlist,file, & odata(216,i),psval,odata(35,i) 40 format(a6,i3,x,'19',i2,x,i2,x,i2,x,f14.5,x, & f14.5,x,f14.5) enddo 200 continue goto 1000 50 num_rec1=num_rec-8+k1 write(2,60) num_rec1 write(3,60) num_rec1 60 format(1x,'Error at record ',i6,/) goto 55 51 num_rec2 = num_rec-8+k2 write(2,60) num_rec2 write(3,60) num_rec2 55 write(3,70) bad_data 70 format(x,960(x,a70,/)) 1000 continue 2000 write(6,80) num_com,num_rec 80 format(x,'# of comps= ',i6,2x,'Number of records = ',i6) close(unit=1) close(unit=2) close(unit=3) stop end