Announcement

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

  • Keeping different observations with different ranges

    Hi Everyone,

    I am super new to Stata like 2 weeks, and I have a pretty big data set in which I need to keep different observations defined in different ranges. I was wondering how can I do it in one time but not every single one of them separately. Because if I use the keep command everything else is getting dropped and I do not want to start again for each observation. The data set looks like this

    id . num_date sales
    1 . 179
    1 . 180
    1 . 181
    1 . 182
    1 . 183
    1 . 184
    2 . 179
    2 . 180
    2 . 181
    2 . 182

    So I wanna keep the observations with num_date 180-184 for id 1, but 179-181 for the id 2. Is there a loop or anything I can create to make everything in one go? If not what is the most convenient way of doing it?

    Thanks
    Last edited by Beste KAYGISIZ; 13 Jul 2018, 12:14.

  • #2
    Hello Beste. There would have to be a general rule that applies the same way for all values of id to achieve this with one command. Is there such a general rule? If so, I can't work it out from what you've said in #1.

    HTH.
    --
    Bruce Weaver
    Email: bweaver@lakeheadu.ca
    Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
    Stata version: 15.1 IC (Windows)

    Comment


    • #3
      General rule? You mean something like another variable that is consistent across the board for all observations?

      Comment


      • #4
        Without some sort of rule suggested by Bruce in #2, you can issue the drop command instead of keep:

        Code:
        drop if inrange(num_date, 180, 184) & id==1
        drop if inrange(num_date, 179, 181) & id==2
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          By rule, I mean I would need to know which values of num_date you want to drop for each id. Is it based on the values of another variable? Is it particular sequence based on a formula?
          Stata/MP 14.1 (64-bit x86-64)
          Revision 19 May 2016
          Win 8.1

          Comment


          • #6
            Thanks for jumping in, Carole. I think you had a clearer grasp of the original question than I did. Using drop rather than keep will indeed solve the problem.

            In #1, Beste said:
            So I wanna keep the observations with num_date 180-184 for id 1, but 179-181 for the id 2.
            So I think you need !inrange in #4, like this:

            Code:
            drop if !inrange(num_date, 180, 184) & id==1
            drop if !inrange(num_date, 179, 181) & id==2
            Beste: An exclamation mark (!) is the symbol for 'not' in Stata code.
            --
            Bruce Weaver
            Email: bweaver@lakeheadu.ca
            Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
            Stata version: 15.1 IC (Windows)

            Comment


            • #7
              Bruce is correct—the “!” is needed before inrange().
              Stata/MP 14.1 (64-bit x86-64)
              Revision 19 May 2016
              Win 8.1

              Comment


              • #8
                Thank you all for the replies, I have figured out the missing ! , so no worries. The only rule is the range is going be 4 for each, like 175- 179, 182-186; however, there is no rule when it comes to what determines the starting number. The last numbers in each range correspond to patent expiration dates of certain drugs, hence there is no rule, unfortunately. Thus what I am wondering is for such cases do we need to write everything one by one for each id?

                Is there a way I can log in the patent expiration dates in order like 179(for id1), 186(for id2), and so on for each of the ids and Stata will directly apply the ranges (t-4,t) to its corresponding id's and drop everything else?
                Last edited by Beste KAYGISIZ; 14 Jul 2018, 00:30.

                Comment


                • #9
                  Yes, it is likely that you can use that information to get what you want in a few lines. Please include a sample of your data with the patent date info using the dataex command (see the 12.2 of the FAQ).
                  Stata/MP 14.1 (64-bit x86-64)
                  Revision 19 May 2016
                  Win 8.1

                  Comment

                  Working...
                  X