Community
cancel
Showing results for 
Search instead for 
Did you mean: 
heini77
Beginner
29 Views

Read binary file

I am working with a microsimulation model written in Fortran. The statistical analyses however are performed with stata. So in order to simulate with the model I first have to convert a STATA-file into a binary file for Fortran. The following code in Fortran does this trick:

OPEN (1,file="e:\\data\\testdef.txt",form='formatted',status='old',access='sequential')
OPE(2, file="e:\\data\\won05.testdef",access='sequential', form='unformatted', recordtype='fixed',status='replace',recl=410)

DO i=1,150000
READ (unit=1,fmt=2001,end=31) (var(j),j=1,480)
WRITE (2) (jint(var(j)),j=1,20),(iint(var(j)),j=21,30),(iint(var(j)),j=31,100),(var(j),j=101,270),(iint(var(j)),j=271,330),(var(j),j=331,480)
END DO

31 CLOSE (1)
CLOSE (2)

So the file "testdef.txt" is a text file created with STATA (and can be opened with a text editor as well) while the second file "won05.testdef" is the file with the correct format for the simulation model written in Fortran. The next step is make several simulations with the modelwhich result in an output file (simular to the file"won05.testdef"). The final step is to convert this file back to a file that can be handled with STATA again. Unfortunately, I was not able to complete this last step. Does anybody have usefull tips?

Gr. Henk-Wim

0 Kudos
2 Replies
WSinc
New Contributor I
29 Views

Have you looked at opening the file with the STREAM keyword?

That basically ignores any file structure, nor does not create any when writing it out.
So just use sequential READs and WRITEs to transfer in or out the raw data bytes.

That's what I use with MIDI files anyway, since they have a proprietary format.

When You're working with the STATA file you of course have to be cognizant about the
way the individual data items are represented, i.e little or big ENDIAN first, signed or unsigned, etc.

Another possibility is to open it with a DIRECT ACCESS qualifier, but that's a little more trouble when doing the I/O.
0 Kudos
heini77
Beginner
29 Views

Thanks for your reply!

I already tried it with the STREAM keyword and unfortunately it did not work either. The same holds for opening the file with a DIRECT ACCESS qualifier. The problem is that the converted file is a binary file as well. I have added these files as attachments.

The first file (testdef.txt) is created with STATA and can be opened with text editors as well. The FORTRAN code posted in the threat converts this file into a binary file: won05.testdef. As a final step, I tried to convert the file back into STATA format. This file (testdef2.txt) is still binary, whereas I need this file to have the same format as the original file (testdef.txt). Any help?
0 Kudos