Announcement

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

  • dropping observations if variable is zero between time period

    Hello,

    Could someone please help me dropping some observations? I'm investigating the influence of patenting on the return on investment of firms. Now I want to drop the companies that never had a patent between variable fyear (fiscal year) 1997-2015 (see the example company in the screenshot). But I want to keep them if they had a patent in this time period. So, also if they only have 1 patent in that time period, I want to keep them in my dataset. I'm struggling to find the right code so could someone please help me?

    Thanks!
    Click image for larger version

Name:	Schermafbeelding 2021-05-17 om 13.53.16.png
Views:	1
Size:	745.5 KB
ID:	1610073

  • #2
    So in this case companies like RELM WIRELESS CORP i want to drop

    Comment


    • #3
      Something like the following might do what you want.
      Code:
      by gvkey (fyear), sort: egen tokeep = max(cond(inrange(fyear,1997,2015),pat_year,0)
      drop if tokeep==0
      To understand the code,
      Code:
      help cond()
      help egen
      help by

      Comment


      • #4
        Thank you for responding. Now I'm getting a error called 'parentheses unbalanced' r132. I tried a few thing but couldn't make it work. Sorry I'm really a nood in stata

        Comment


        • #5
          Here is how you diagnose the source of unbalanced parentheses —the fact that there are three left parentheses but just two right parentheses.
          Code:
          max(cond(inrange(fyear,1997,2015),pat_year,0))
                          |_______________|
          Does help inrange() tell us this is the correct syntax for the inrange() function?
          Yes.
          Code:
          max(cond(inrange(fyear,1997,2015),pat_year,0)
                          |_______________|
                  |___________________________________|
          Does help cond() tell us this is the correct syntax for the cond() function?
          Yes.
          Code:
          max(cond(inrange(fyear,1997,2015),pat_year,0)
                          |_______________|
                  |___________________________________|
             |__________________________________________ no matching )
          So the max() function is missing its right parenthesis.
          Code:
          by gvkey (fyear), sort: egen tokeep = max(cond(inrange(fyear,1997,2015),pat_year,0))
          drop if tokeep==0

          Comment


          • #6
            Thank you for your clear response! I understand it now. The code worked!

            Comment

            Working...
            X