• Laura Whiting

The file Command - Reading and Writing Text and Binary Files

Updated: Aug 27, 2020

The file command in Stata allows you to read and write simple text files. It allows you to load a text file into Stata in one of three modes: read, write, or read and write. Once the file is loaded, you can then either read or write to that file (or both) depending on which mode you selected when the file was opened. You can open a text file that already exists, or if it doesn’t exist then opening it in Stata will create the text file.

This function can be used to easily take notes as you go through an analysis, without ever having to leave Stata. It can also be used when developing a program. For example, it could be used in a simple user management program for network licences installed on a server. These examples are expanded on below.

How to Use:

Where options are listed within brackets and between lines | below, choose the option you need. For example – (read | write | read write) – specify only one of either read or write or read write.

*Open a new or existing text file in read, write or read & write mode. The replace option is needed when writing to an existing text file as you will erase what was already there.

*Read one line of an opened file (do this multiple times to read the whole file).

*Write to an opened file

*Move to a particular point in the file (tof = top of file, eof = end of file, # = replace with specific byte number)

*Close and save a file

Worked Example 1 - Taking Notes:

Opening and adding to a text file can be a quick and easy way of taking notes as you run through an analysis. While Stata does have its own notes command for datasets, these notes are recorded as part of the dataset. Notes taken using the file command can be easily viewed and shared without the need to share the dataset. You can also automate notes taken with the file command, allowing you to record the same type of note with new information depending on the dataset. The example below is a small do-file that could be run whenever a new dataset was loaded into Stata:

Download TXT • 298B

To read this file in Stata I use the following commands:

This shows me the following output:

Worked Example 2 - Recording the Number of Users Currently Connected to a Network:

This example is a really simply way of keeping track of how many users are on your network. The first step is to create a text file containing only the number 0, and save this to a location on the network that Stata can access upon startup. I have called my text file people.txt and it is saved in the folder Stata Network Logs on our server. You then add the following lines of code to each user’s profile.do saved in their User folder:

This will increase the number in the text file people.txt by 1 every time a user opens their Stata. However, this is not enough. You will also need to decrease the number every time a user closes their Stata. To do this you create a small close.ado file which is added to each users personal ado path. The close.ado file has the following:

Now when a user exits Stata, instead of typing exit in the command line or clicking the X in the corner of the screen, they type close into the command line instead. This ensures the close.ado file is run and the program close will incrementally reduce the number in people.txt before exiting Stata automatically. By increasing the number when Stata is opened through profile.do and decreasing the number when Stata is closed through close.ado you can accurately track how many people are currently using Stata at any time by simply opening the people.txt and looking at the number recorded there.

This trick relies on your users remembering to shut down their Stata with the special command close rather than clicking the X or using exit.

109 views0 comments

Recent Posts

See All