Announcement

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

  • Dropping observations based on multiple conditions

    Hi,

    My data consists of multiple rows for each person. I want to keep the rows if the followup=0 or if followup is between 2.5 and 3.5.

    My data looks like this:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    * dataex  naccid Followup
    clear
    input str10 naccid float Followup
    "NACC000011"          0
    "NACC000011"   1.169863
    "NACC000011"   2.131507
    "NACC000011"    3.29863
    "NACC000271" -1.9013698
    "NACC000271"  -.9178082
    "NACC000271"          0
    "NACC000271"        1.2
    "NACC000271"  2.2931507
    "NACC000271"   3.410959
    "NACC000304"          0
    "NACC000304"   .9972603
    "NACC000304"  2.0465753
    "NACC000304"   2.972603
    "NACC000304"   3.991781
    "NACC000304"   5.021918
    "NACC000304"   6.364384
    "NACC000382"          0
    "NACC000382"          1
    "NACC000382"  1.9945205
    "NACC000382"   3.068493
    "NACC000382"  4.5068493
    end
    I have used the following commands but it does not seem to work:
    drop if Followup <!2.5>!3.5 & Followup !=0
    drop if Followup <2.5 | Followup >3.5 | Followup !=0

    So essentially for NACC000011, I want to keep rows 1 and 4; and for NACC000271 rows 7 and 10, for NACC000304 rows 11 and 14 so on...

    Any suggestions?

  • #2
    Perhaps one of these, which seem more natural expressions of the conditions you have described.
    Code:
    keep if Followup==0 | (Followup>=2.5 & Followup<=3.5)
    keep if Followup==0 | inrange(Followup,2.5,3.5)
    To demonstrate that it works
    Code:
    . generate keep_1 = Followup==0 | (Followup>=2.5 & Followup<=3.5)
    
    . generate keep_2 = Followup==0 | inrange(Followup,2.5,3.5)
    
    . list, clean noobs
    
            naccid    Followup   keep_1    keep_2  
        NACC000011           0         1        1  
        NACC000011    1.169863         0        0  
        NACC000011    2.131507         0        0  
        NACC000011     3.29863         1        1  
        NACC000271    -1.90137         0        0  
        NACC000271   -.9178082         0        0  
        NACC000271           0         1        1  
        NACC000271         1.2         0        0  
        NACC000271    2.293151         0        0  
        NACC000271    3.410959         1        1  
        NACC000304           0         1        1  
        NACC000304    .9972603         0        0  
        NACC000304    2.046575         0        0  
        NACC000304    2.972603         1        1  
        NACC000304    3.991781         0        0  
        NACC000304    5.021918         0        0  
        NACC000304    6.364384         0        0  
        NACC000382           0         1        1  
        NACC000382           1         0        0  
        NACC000382    1.994521         0        0  
        NACC000382    3.068493         1        1  
        NACC000382    4.506849         0        0

    Comment

    Working...
    X