Transcription of Title stata.com drop — Drop variables or …
1 drop variables or observationsSyntaxMenuDescriptionRemarks and examplesReferenceAlso seeSyntaxDrop variablesdropvarlistDrop observationsdrop ifexpDrop a range of observationsdrop inrange[ifexp]Keep variableskeepvarlistKeep observations that satisfy specified conditionkeep ifexpKeep a range of observationskeep inrange[ifexp]byis allowed with the second syntax ofdropand the second syntax ofkeep; see [D] or keep variablesData> variables ManagerDrop or keep observationsData>Create or change data> drop or keep observationsDescriptiondropeliminates variables or observations from the data in the same way asdrop, except that you specify the variables or observations to bekept rather than the variables or observations to be drop drop variables or observationsWarning:dropandkeepare not reversible.
2 Once you have eliminated observations, you cannotread them back in again. You would need to go back to the original dataset and read it in of applyingdroporkeepfor a subset analysis, consider usingiforinto select subsetstemporarily. This is usually the best strategy. Alternatively, applyingpreservefollowed in due coursebyrestoremay be a good and can clear the entire dataset by typingdropallwithout affecting value labels, macros, andprograms. (Also see[U] Dataset, variable , and value labels,[U] Macros, and [P]program.)Example 1We will systematically eliminate data until, at the end, no data are left in memory.
3 We begin bydescribing the data:. use (1980 Census data by state). describeContains data from : 50 1980 Census data by statevars: 15 6 Apr 2013 15:43size: 3,300storage display valuevariable name type format label variable labelstate str14 %-14s Statestate2 str2 %-2s Two-letter state abbreviationregion int % cenreg Census regionpop long % Populationpoplt5 long % Pop, < 5 yearpop5_17 long % Pop, 5 to 17 yearspop18p long % Pop, 18 and olderpop65p long % Pop.
4 65 and olderpopurban long % Urban populationmedage float % Median agedeath long % Number of deathsmarriage long % Number of marriagesdivorce long % Number of divorcesmrgrate float % float % by: regionWe can eliminate all the variables with names that begin withpopby typingdrop pop*: drop drop variables or observations 3. drop pop*. describeContains data from : 50 1980 Census data by statevars: 9 6 Apr 2013 15:43size.
5 2,100storage display valuevariable name type format label variable labelstate str14 %-14s Statestate2 str2 %-2s Two-letter state abbreviationregion int % cenreg Census regionmedage float % Median agedeath long % Number of deathsmarriage long % Number of marriagesdivorce long % Number of divorcesmrgrate float % float % by: regionNote: dataset has changed since last savedLet s eliminate more variables and then eliminate observations.
6 drop marriage divorce mrgrate dvcrate. describeContains data from : 50 1980 Census data by statevars: 5 6 Apr 2013 15:43size: 1,300storage display valuevariable name type format label variable labelstate str14 %-14s Statestate2 str2 %-2s Two-letter state abbreviationregion int % cenreg Census regionmedage float % Median agedeath long % Number of deathsSorted by: regionNote: dataset has changed since last savedNext we willdropany observation for whichmedageis greater than drop if medage>32(3 observations deleted)Let s drop the first observation in each region.
7 By region: drop if _n==1(4 observations deleted)Now we drop all but the last observation in each region:. by region: drop if _n !=_N(39 observations deleted)Let s now drop the first 2 observations in our dataset:. drop in 1/2(2 observations deleted)4 drop drop variables or observationsFinally, let s get rid of everything:. drop _all. describeContains dataobs: 0vars: 0size: 0 Sorted by:Typingkeep in 10/lis the same as typingdrop in 1 if x==3is the same as typingdrop if x != especially useful for keeping a few variables from a large dataset. Typingkeep myvar1myvar2is the same as typingdropfollowed by all the variables in the noteIn addition to dropping variables and observations,dropallremoves any business calendars;see [D]datetime business , N.
8 J. 2001. dm89: Dropping variables or observations with missing Technical Bulletin60: 7 inStata Technical Bulletin Reprints, vol. 10, pp. 44 46. College Station, TX: Stata see[D]clear Clear memory[D]varmanage Manage variable labels, formats, and other properties[U] 11 Language syntax[U] 13 Functions and expressions