UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Class Notes
Splitting & Combining Files


1.0 Stata commands in this unit

. drop cases
keep cases
append
drop variables
keep variables
sort
merge

2.0 Demonstration and Explanation

2.1 Create two new datasets by dropping cases

. use hsb2, clear
drop if female==0
save hsbf

tabulate female

. use hsb2, clear
drop if female==1
save hsbm
tabulate female

2.2 Create two new datasets by keeping cases

The exact same thing can be accomplished using the keep command instead of the drop command.

. use hsb2, clear
keep if female==0
save hsbm, replace
tabulate female

. use hsb2, clear
keep if female==1
save hsbf, replace
tabulate female

2.3 Create a single dataset by appending

. use hsbm, clear
append using hsbf

. tabulate female

2.4 Create two new datasets by dropping variables

. use hsb2, clear
drop female-prog
sort id
save hsbv1
describe

. use hsb2, clear
drop read-socst
sort id
save hsbv2
describe

2.5 Create two new datasets by keeping variables

As before, the exact same thing can be accomplished using the keep command instead of the drop command.

. use hsb2, clear
keep id read-socst
sort id
save hsbv1
describe

. use hsb2, clear
keep id female-prog
sort id
save hsbv2
describe

2.6 Create a single dataset by merging

. use hsbv1, clear
merge id using hsbv2
describe

3.0 Try the commands on your own

. use hsb2, clear
drop if female~=0
save hsbf, replace
use hsb2, clear
drop if female~=1
save hsbm, replace
append using hsbf

4.0 Can you answer this questions?

  1. What would happen if you did the following?:
    use hsb2, clear
    append using hsb2

5.0 For More Information

6.0 Web Notes

The Stata Class Notes are available on the World Wide Web by visiting ...
      http://www.ats.ucla.edu/stat/stata/notes/

The dataset hsb2.dta can be loaded directly into Stata, over the Internet, using the following commands:
use http://www.ats.ucla.edu/stat/stata/notes/hsb2


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.