|
|
|
||||
|
|
|||||
. drop cases
keep cases
append
drop variables
keep variables
sort
merge
. use hsb2, clear
drop if female==0
save hsbf
. use hsb2, clear
drop if female==1
save hsbm
tabulate female
We started with hsb2 which had 200 cases and created two new datasets hsbm and hsbf using the drop command. hsbm has only males (n = 91) while hsbf has only females (n = 109).
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
Note that drop if female==0 is equivalent to keep if female==1.
. use hsbm, clear
append using hsbf
. tabulate female
The append command concatenates two datasets, that is, sticks them together vertically, one after the other. Normally, the append command would be followed by a save command, however in this case, we already have the hsb2 dataset which is the same as hsbm and hsbf appended together.
. use hsb2, clear
drop female-prog
sort id
save hsbv1
describe
. use hsb2, clear
drop read-socst
sort id
save hsbv2
describe
We started with hsb2 which had 200 cases and 11 variables. Using the drop command we created hsbv1 which has six variables and hsbv2 which also has six variables. The variable id appears in both datasets.
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
This time drop female-prog is equivalent to keep read-socst.
. use hsbv1, clear
merge id using hsbv2
describe
The merge command sticks two datasets together horizontally, one next to the other. Both datasets must be sorted by the merge variable, in this case, id, before being merged. Normally, the merge command would be followed by a save command, however in this case, we already have the hsb2 dataset which is the same as hsbv1 and hsbv2 merged by id.
. use hsb2, clear
drop if female~=0
save hsbf, replace
use hsb2, clear
drop if female~=1
save hsbm, replace
append using hsbf
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
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