Minot
Using Stata for Survey Analysis
Page 38
SECTION 8: MODIFYING DATA FILES
This section describes a number of commands that are used to modify and combine data files in Stata.
We begin with a five simple commands and then move to five more complex ones.
rename
drop
keep
sort
compress
collapse
merge
append
reshape
fillin
rename
This command renames variables. Some examples:
rename oldname newname
rename b21_q1 sexhead
rename b21_q3ag agehead
drop
This command deletes records or variables. Examples are:
drop if agehead>140
deletes records in which age is greater than 140
drop if area==.
deletes records in which area is missing
drop temp1 temp2
deletes variables temp1 and temp2
keep
This command deletes everything but specified observations or variables. Examples include:
keep if agehead <= 140
keeps only records in which age is 140 or under
keep hhid agehead pcexpend
keeps only variables hhid, agehead, and pcexpend,
deleting all others
sort
This command sorts the records in the file according to the value of specified variables. It is the same
as “sort cases” in SPSS. Examples are:
sort region town
sorts data file in order of region and town
sort urban
sorts by the dummy variable urban
compress
This command reduces the size of the file by changing the data storage types.
It will not make any
changes that would cause Stata to lose data. This command has no options or arguments.