Skip to content. | Skip to navigation

Personal tools

Stata temporary files

How to create temporary work datasets in Stata (like datasets in the work library in SAS). These datasets can be used, saved, and modified. At the end of the program when Stata is exited they will be discarded automatically by Stata (just like in SAS).

This example is more a Stata 8 example. In Stata 9 the -merge- command can sort the using datasets if requested to do so. So the following code could be as simple as this:

use "data/snak9609.dta"
merge hhid personid using "csf96/data/csf9605b.dta" keep(hhid personid stratum psu wt3_day1)


// Tell Stata the name(s) of the temporary datasets you are going
// to use in your program.  You can do one at a time or many at a time: 

tempfile design    

// load the permanent Stata dataset into memory and keep only the sample design correction variables 
use  hhid personid stratum psu wt3_day1 using "csf96/data/csf9605b.dta"

// sort the dataset by hhid and personid
sort hhid personid

// save the Stata dataset in the temporary file named "design"
// NOTE:  the first quote is a left-hand quote.  It is on the the button next to the
//   1/! key at the top left of your keyboard.  The second quote is a normal single quote. 
save "`design'"

// load the other dataset into memory
use "data/snak9609.dta"

// sort it by hhid and personid 
sort hhid personid

// merge the current dataset in memory (snak9609.dta) with the temporary dataset `design' 
merge hhid personid using "`design'"

// tabulate the _merge variable that is created by the merge command 
tab _merge

************************************************************
This is the same code without all the comments:

tempfile design;

use hhid personid stratum psu wt3_day1 using "csf96/data/csf9605b.dta"
sort hhid personid
save "`design'"

use "data/snak9609.dta"
sort hhid personid

merge hhid personid using "`design'"
tab _merge