Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Delete specific values

    Hi Statalist,

    I'm trying to delete observations that meet a certain condition, but I can't find any command that can do this.

    My sample is as follows
    Code:
    ID Characteristic
    1   a
    1   b
    2   a
    3   c
    3   c
    I want to delete
    (1) all observations with Characteristic a
    (2) all observations with the same id as the observations with Characteristic a

    I know it is possible to do (1) with code like this
    Code:
    drop if Characteristic == ( a )
    But I don't know how to do (2)

    thanks for help.
    Last edited by Ding-Dang Yang; 26 Apr 2022, 23:25.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id str1 characteristic
    1 "a"
    1 "b"
    2 "a"
    3 "c"
    3 "c"
    end
    
    by id, sort: egen to_drop = max(characteristic == "a")
    drop if to_drop
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thanks for your help, I'll pay attention to the data format part in the future.
      This is the first time I try to communicate with people programmatically, but I don't know much about it.
      Thank you very much for your tolerance and detailed explanation!

      Comment

      Working...
      X