#eye #eye

How to use multiple frames in Stata


Welcome to the Stata Guide on saving multiple frames in memory in Stata! This is helpful for when you are interested in switching back and forth between multiple datasets while in the same session.

Throughout this guide, blue phrases will be used to indicate commands, green phrases for variables, and purple for links and downloads. 

We’ll be using two of Stata’s built-in datasets, lifeexp.dta and uslifeexp.dta to explore how it might be helpful to switch between two separate but related dataframes. The first is a general dataset comparing life expectancy around the world. The second, is US specific.


Here is the .do file which you can use to follow the guide with.


Begin by opening Stata and a .do file editor window.
 

The Default Frame


Stata automatically generates a frame as you begin to work and open a dataset. This can be referred to as the ‘default frame’. Let’s begin by loading in the first dataset:

sysuse lifeexp, clear
We can observe that the data has loaded into Stata’s main window and the variables have populated their respective tab.



Next, we will want to rename the frame that we have loaded our life expectancy data into. In Stata, this is considered the ‘default’ frame since it is the one that is automatically loaded without having to set it.


Let’s rename this ‘default’ frame ‘global’ and then double check that it has been updated by displaying the current frame, or c(frame).

frame rename default global

display c(frame)



We can see that the renaming has occurred as we thought it would since the first line threw no error and the second returned ‘global’. Next, let’s move on to creating a second frame.


We will name this frame US since this is where we will place the dataset that only contains information on the US. Let’s run the following:

frame create us

A frame has now been created to be called ‘us’. Even after creating a second frame, we are still working in the first one which we titled ‘global’. We can switch to our second frame by running the following:

frame change us

To double check that this has occurred, let’s run display c(frame) again.

display c(frame)


As of currently, this frame should be empty but we can update it to include the US life expectancy Stata file by running the following:

sysuse uslifeexp, clear



The data has now been loaded and populated into the ‘us’ frame.


But, we might want to check all the frames that Stata has in its memory. To do so, we will check the directory by running:

frames dir



We can now see our two frames with their respective titles and sizes. You have now successfully made multiple frames, loaded in different datasets, and learned how to switch between them!





Congrats on making it to the end of this ERC Stata How-To Guide!



For more How-Tos on using Stata see here:

  • How to: Reclassifying Variables in Stata
  • How to: Create Multiplots in Stata
  • How to: Clean Survey Data in Stata
  • How to: Use Appending and Merging Data in Stata