Announcement

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

  • generate a dummy varaible based on whetehr the other dummy has the same or more ==1 than ==0 over the sample period

    Hello

    I have a panel data with firm and year variables. I also have a dummy variable, litigation, which equals 1 if there are 1 or more litigations for a firm in a year and 0 if there is no litigation for the firm in that year. I want to create a new dummy variable, abovemedian_litigation, which equals 1 if the firm has 50% or more of the times with litigation==1 than litigation==0 over the sample period. How can I do this? Thanks a lot for any suggestions!
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float year double firm float litigation
    2010  2 1
    2011  2 1
    2012  2 1
    2013  2 1
    2014  2 0
    2015  2 0
    2016  2 1
    2017  2 1
    2018  2 1
    2019  2 1
    2010  4 0
    2011  4 0
    2012  4 1
    2013  4 1
    2014  4 1
    2015  4 0
    2016  4 1
    2017  4 0
    2018  4 1
    2019  4 1
    2010  5 1
    2011  5 1
    2012  5 1
    2013  5 1
    2014  5 1
    2015  5 0
    2016  5 1
    2017  5 1
    2018  5 1
    2019  5 1
    2011  7 0
    2012  7 0
    2013  7 1
    2014  7 0
    2015  7 1
    2016  7 0
    2017  7 0
    2018  7 0
    2019  7 1
    Last edited by Alice Yang; 01 Apr 2022, 00:41.

  • #2
    Code:
    egen wanted = max(sum(litigation)/_N>=.5), by(firm)

    Comment


    • #3
      Hi Øyvind,

      Thanks a lot for your quick reply and help. I tried your code but there may be something wrong? Such as firm 2, there are 8 1s and 2 0s for litigation, so the firm has more years with litigation than without, then the wanted variable should be coded 1 for firm 2 from 2010-2019, however if I use the code above, it gives 0 instead. Can you please kindly have another look at the code? Thanks!

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(year firm litigation wanted)
      2010 2 1 0
      2011 2 1 0
      2012 2 1 0
      2013 2 1 0
      2014 2 0 0
      2015 2 0 0
      2016 2 1 0
      2017 2 1 0
      2018 2 1 0
      2019 2 1 0
      2010 4 0 0
      2011 4 0 0
      2012 4 1 0
      2013 4 1 0
      2014 4 1 0
      2015 4 0 0
      2016 4 1 0
      2017 4 0 0
      2018 4 1 0
      2019 4 1 0
      2010 5 1 1
      2011 5 1 1
      2012 5 1 1
      2013 5 1 1
      2014 5 1 1
      2015 5 0 1
      2016 5 1 1
      2017 5 1 1
      2018 5 1 1
      2019 5 1 1
      2011 7 0 1
      2012 7 0 1
      2013 7 1 1
      2014 7 0 1
      2015 7 1 1
      2016 7 0 1
      2017 7 0 1
      2018 7 0 1
      2019 7 1 1
      end

      Comment


      • #4
        Code:
        . bysort firm : egen median = median(litigation)
        
        . gen wanted = median == 1 
        
        . tabdisp firm, c(wanted)
        
        ----------------------
             firm |     wanted
        ----------+-----------
                2 |          1
                4 |          1
                5 |          1
                7 |          0
        ----------------------

        Comment


        • #5
          Hi Nick,

          Thanks for your help! I noticed that if there are equal numbers of 1s and 0s in litigation for a firm in the sample period, this method will assign wanted 0. I'd like wanted to be 1 if there are 50% or more of the 1s in litigation, how can the code be revised? Thanks!

          Comment


          • #6
            You're right. The median will be 0.5 with equal numbers of 1s and 0s.

            median >= 0.5

            Comment


            • #7
              Hi again,

              I have another question regarding this post. So in #1, for each firm, I want to account for whether more than 50% of the years the firm has litigation==1 than litigation==0 over the entire sample period. Now I have a new variable, eventyear, to indicate the year a event happens to a firm. If a firm is in the treatment group, the eventyear variable will indicate the year it has been treated. Different firms are being treated at different years, and a firm can only be treated once during the sample period. Other firms, namely those in the control group, will never be treated and have no value for the eventyear variable.

              I want to create a dummy variable to indicate:
              - For treatment firms, account for the years prior to the eventyear only, whether more than 50% of the years the firm has litigation==1 than litigation==0.
              - For control firms, same as in #1, account for all years in the sample period, whether more than 50% of the years the firm has litigation==1 than litigation==0.

              How can the code be revised? Thakns a lot for helping me again!

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float(year firm litigation eventyear)
              2010 2 1 2013
              2011 2 1 2013
              2012 2 1 2013
              2013 2 1 2013
              2014 2 0 2013
              2015 2 0 2013
              2016 2 1 2013
              2017 2 1 2013
              2018 2 1 2013
              2019 2 1 2013
              2010 4 0    .
              2011 4 0    .
              2012 4 1    .
              2013 4 1    .
              2014 4 1    .
              2015 4 0    .
              2016 4 1    .
              2017 4 0    .
              2018 4 1    .
              2019 4 1    .
              2010 5 1 2015
              2011 5 1 2015
              2012 5 1 2015
              2013 5 1 2015
              2014 5 1 2015
              2015 5 0 2015
              2016 5 1 2015
              2017 5 1 2015
              2018 5 1 2015
              2019 5 1 2015
              2011 7 0    .
              2012 7 0    .
              2013 7 1    .
              2014 7 0    .
              2015 7 1    .
              2016 7 0    .
              2017 7 0    .
              2018 7 0    .
              2019 7 1    .
              end

              Comment


              • #8
                Anyone can kindly help me? Thanks!

                Comment

                Working...
                X