Announcement

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

  • Drop Command with Multiple If Statements

    Hi there,

    I am working with hospital data that spans a few years and need to drop patients that have their first visit in 2017. I've sorted my data by ID and date and used the egen tag = tag (ID) command to tag the first observation for each participant.

    This is the code I'm working but unfortunately it's dropping both tag == 1 AND Date >=2017, not the combination of the two. I.e. it's dropping way too many observations.

    Code:
     drop if tag == 1 & Date >=2017
    Please advise how to properly combine multiple if statements when using the drop command.

    Thank you!

    - K

  • #2
    It seems to perform as expected for me.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id tag Date)
    101 1 2016
    102 0 2017
    103 1 2017
    666 1 .
    end
    drop if tag == 1 & Date >=2017
    list, clean
    Code:
    . drop if tag == 1 & Date >=2017
    (2 observations deleted)
    
    . list, clean
    
            id   tag   Date  
      1.   101     1   2016  
      2.   102     0   2017
    Note that id 666 is dropped because in a comparison, missing values are considered greater than any nonmissing value. Perhaps you have some some observations with Date missing, and those are the extra observations being dropped. Or perhaps tag was generated incorrectly.

    Added in edit: having read your question more carefully I now see

    I ... need to drop patients that have their first visit in 2017.
    Your code will not achieve this, because it is only going to drop the first observation for the patient, not every observation for the patient. What you need is
    Code:
    bysort id (Date): drop if Date[1]>=2017
    Last edited by William Lisowski; 19 Jun 2018, 20:01.

    Comment


    • #3
      Hi William,

      Thank you for your reply.

      When I used the
      Code:
       
       bysort id (Date): drop if Date[1]>=2017
      that you suggested it ends up dropping all of my observations. Do you know why this might be?

      To respond to your previous concerns, I also do not have any Dates missing in my dataset.

      - K

      Comment


      • #4
        When I used the [code] that you suggested it ends up dropping all of my observations. Do you know why this might be?
        I can't know, but I can guess it's because your Date variable differs in some significant way from the Date variable I imagined. Do you know what this might be? Because without seeing your data, I can only guess what its like, and clearly I haven't guessed correctly in post #2.

        Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

        All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        If after that reading you are still unsuccessful in adapting the code I suggest to work with your actual data, then post an example of your data set using the dataex command. If you are running Stata version 15.1, then dataex is part of your installation already. If running an earlier Stata, run ssc install dataex to get the command. Either way, run help dataex to read the simple instructions for using it. It takes just seconds to use dataex to provide a way for those who want to help you to create a complete and faithful replica of your example data in their own Stata. That, in turn, eliminates all sorts of questions that are left unanswered by descriptions or even by data listings and tables. And it enables people to test out code, so that you get the right answer the first time.

        And to improve future posts, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

        The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

        Comment

        Working...
        X