Announcement

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

  • drop observations

    Hello! I am new in STATA. Please help me with this issue
    I need to drop observations with "choice"
    Record_id Trial Alternative Info_obs
    8 2 2 choice
    8 3 1 choice
    9 1 1 rand
    9 1 2 rand
    9 2 1 rand
    Here is variable info:

    storage display value
    variable name type format label variable label
    ------------------------------------------------------------------------------------------------------------
    Info_obs str12 %12s Info_obs

    I would appriciate any help.

  • #2
    This works for the data you've posted. I've added trim to deal with unwanted spaces.

    Code:
    drop if trim(Info_obs) == "choice"
    Last edited by Justin Niakamal; 17 Feb 2019, 20:34.

    Comment


    • #3
      Just an alternate in case the variable may contain more than the word choice in it:

      Code:
      dataex id trial text_var  // data shared using -dataex-. To install: ssc install dataex
      clear
      input byte(id trial) str12 text_var
       8 2 "choice"      
       8 3 "my choice"  
       9 1 "his choice"  
       9 1 "not a choice"
       9 2 "rand"        
      10 2 "random"      
      10 2 "my random"  
      10 3 "his random"  
      end
      ------------------ copy up to and including the previous line ------------------
      
      . list, sepby(id) noobs
      
        +---------------------------+
        | id   trial       text_var |
        |---------------------------|
        |  8       2         choice |
        |  8       3      my choice |
        |---------------------------|
        |  9       1     his choice |
        |  9       1   not a choice |
        |  9       2           rand |
        |---------------------------|
        | 10       2         random |
        | 10       2      my random |
        | 10       3     his random |
        +---------------------------+
      
      gen contains_choice = (strpos(text_var, "choice") > 0)   // 1 if text_var contains "choice" anywhere within it
      gen contains_rand   = (strpos(text_var, "rand"))  // Doing it this way returns the actual position at which "rand" is first found
      
      . list, sepby(id) noobs abbrev(18)
      
        +-------------------------------------------------------------+
        | id   trial       text_var   contains_choice   contains_rand |
        |-------------------------------------------------------------|
        |  8       2         choice                 1               0 |
        |  8       3      my choice                 1               0 |
        |-------------------------------------------------------------|
        |  9       1     his choice                 1               0 |
        |  9       1   not a choice                 1               0 |
        |  9       2           rand                 0               1 |
        |-------------------------------------------------------------|
        | 10       2         random                 0               1 |
        | 10       2      my random                 0               4 |
        | 10       3     his random                 0               5 |
        +-------------------------------------------------------------+
      
      * NOTE that strpos() is case-sensitive, to make it not, do something like
      gen contains_choice = (strpos(strupper(text_var), strupper("choice")) > 0)
      Last edited by David Benson; 17 Feb 2019, 22:40.

      Comment


      • #4
        nice!! Thank you so much!!!

        Comment

        Working...
        X