Peter Dutton
Peter Dutton

Reputation: 307

(command) if x==list in stata

I want to be able to write an if expression without having to resort to a large number of or "|" operators. For example:

drop if x==1 | x==3 | x==5 | x==7

Is there a simpler way to write this ie:

drop if x==one of (1,3,5,7)

I realize a for loop will work but that isn't a very tidy solution.

Upvotes: 2

Views: 3070

Answers (1)

Metrics
Metrics

Reputation: 15458

You can use inlist for that

sysuse auto



    tab  rep78

     Repair |
Record 1978 |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |          2        2.90        2.90
          2 |          8       11.59       14.49
          3 |         30       43.48       57.97
          4 |         18       26.09       84.06
          5 |         11       15.94      100.00
------------+-----------------------------------
      Total |         69      100.00
keep if inlist(rep78,1,2,3)

 tab  rep78

     Repair |
Record 1978 |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |          2        5.00        5.00
          2 |          8       20.00       25.00
          3 |         30       75.00      100.00
------------+-----------------------------------
      Total |         40      100.00

Upvotes: 2

Related Questions