Search code examples
statadata-management

Stata keeping even years


If I have data for every year of 1932 to 2012, how do I keep only the even number years from 1946 to 2012 in Stata? I've tried the following:

keep if year == 1946(2)2012 

But it doesn't seem to help.


Solution

  • The error you receive with your code is: unknown function 1946(). Stata thinks 1946 is a function because it is followed by an opening parenthesis. It is expecting an expression and functions can be part of an expression. However, you are giving it a numlist (help numlist), and that is not allowed.

    An example that works:

    clear 
    set more off
    
    *----- example data -----
    
    set obs 81
    egen year = seq(), from(1932) to(2012)
    
    list
    
    *----- what you want -----
    
    keep if mod(year,2) == 0 & year >= 1946
    
    list
    

    Note I used a (legal) function, namely, the modulo function.