Temporary files
Saving and using temporary files on disk
In previous examples we often created a file on disk that we needed only for the duration of the example. Usually it was a sorted copy of a permanent file. We erased the file at the end of the example to clean up after ourselves.
Stata supplies the tempfile command to get around creating and erasing permanent files when what we really need are temporary files.
clear use "t:\statatut\exampw1.dta" sort v001 v002 v003 tempfile temp1 /* create a temporary file */ save "`temp1'" /* save memory into the temporary file */ use "t:\statatut\exampw2.dta" sort v001 v002 v003 merge v001 v002 v003 using "`temp1'" /* use the temporary file */
Questions:
1. The name temp1 in the save and merge commands is enclosed in single quotes. Why is that? Answer.
2. Why don't we need to erase temp1 after we're finished with it?
Answer.
Answers:
1. The name "temp1" is actually a temporary variable name, which is called a "macro" in Stata. In this case it refers to a temporary file. Looking in the log you'll see that the actual temporary file is named something like:
C:\TEMP\ST_040001.tmp
The macro "temp1" contains that value. To get that value, we need to put single quotes around the macro name.
Note that you must use the "backward" single quote (on the tilda key) at the beginning of your macro name, and the "forward" single quote (on the double-quotes key) at the end of your macro name when you refer to the file.
Back to question
2. We don't need to erase temp1 because Stata takes care of that for us. In theory, anyway, that's true. Stata creates other tempfiles on its own, and sometimes for various reasons Stata doesn't clean up after itself. So it's a good idea periodically to check the tempfile directory that Stata uses and delete old files with a naming convention like ST_nnnnnn.tmp. In the example above, Stata is using C:\TEMP for tempfiles.
Back to question
Review again?
Another topic?


