Working with frames in Stata

infoart.ca
3 min readJun 24, 2023

--

Photo by Rolands Zilvinskis on Unsplash

One of the most useful tools in Stata 16 (or the newer versions) is the frame command, which allows users to manipulate data frames with ease. For example, they can drop or rename variables within the target datasets or create new variables based on existing ones, and append or merge multiple datasets in memory. They can also filter out observations that don’t meet certain criteria or sort their dataset based on specific variables. In this tutorial, we will explore some of the basic uses of the frame command.

Creating a frame

#load an example data
sysuse auto

#name the frame
frame name df1

# copy the frame
frame copy default df1

# change to the new frame
. frame change df1

Now check the new frame:

. frame
(current frame is df1)

Next, we will see how to append two frames into one. For this purpose, we will create another frame called df2. In order to make them look different, we’ll keep observation where foreign==1 in df1, and foreign==0 in df2

#create df2
. frame copy df1 df2

# switch to df2

#check the frame
. frame
(current frame is df2)

# keep target observations only
keep if foreign==0

Now we will do the same for df1:

# switch to df1
frame change df1

#check the frame
. frame
(current frame is df1)

# keep target observations only
keep if foreign==1

To append the frames, we will make use of the command frameappend from the frameappend package. Let’s now install the package and complete the appending:

# install frameappend
ssc install frameappend

# make sure the frames exist in memory
. frame dir
default 74 x 12; 1978 Automobile Data
* df1 22 x 12; 1978 Automobile Data
* df2 74 x 12; 1978 Automobile Data

Note: frames marked with * contain unsaved data


# now append the current frame (df1) with one in memory (df2)
frameappend df2

# check the foreign variable in the appended frame

. tabulate foreign


# here is how to append frames in memory using loop
. local frames df1 df2 df3 df4

. frame create combined

. frame change combined

. foreach f of local frames {
frameappend `f'
}

Lastly, we’ll see how to merge two dataframes which is used for adding new variables rather than new observations. Merging is done firstly by creating a link between the current and target dataframe (using the frlink command), and then by importing the desired variables from the target dataframe (using the frget command).

. frame copy default df1

. frame dir
default 74 x 12; 1978 Automobile Data
df1 74 x 12; 1978 Automobile Data

#drop some variables fro default
. drop weight length turn displacement gear_ratio foreign

# now link the default with df1 by generating a link name 'lk'

. frlink 1:1 make, frame(df1) gen(lk)
(all observations in frame default matched)

# finally we can start importing/copying variables from the intact dataframe

. frget weight length turn displacement gear_ratio foreign, from(lk)
(6 variables copied from linked frame)

Happy framing!

--

--

infoart.ca
infoart.ca

Written by infoart.ca

Center for Social Capital & Environmental Research | Posts by Bishwajit Ghose, BI consultant and lecturer at the University of Ottawa

No responses yet