Announcement

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

  • How to replace per individual

    For simplicity, assume I have a panel dataset with two firms with 10 years' revenue data (2001-2010).
    For revenue, firm A has one missing value in 2002. Firm B has no missing values.

    I am trying to generate dummy = 1 for all years unless a firm has missing values for all years.
    Basically, I would like to have dummy = 1 for both firms for all years.

    I tried the below, but it only generate dummy = 1 for non-missing values..

    Code:
    gen  dum = 0
    bysort firmid:  replace dum= 1 if revenue !=.

    With the code I have, I see dum = 0 in 2002, I need it to be dum = 1.
    How could we do that?

  • #2
    Code:
    set obs 30
    egen firmid=seq(), from(1) to(3) block(10)
    egen year=seq(), from(2001) to(2010)
    gen revenue=runiform()
    replace revenue=. if firmid==1 & year==2002
    replace revenue=. if firmid==3
    
    misstable summarize revenue, generate(miss_)
    bysort firmid: gen t_obs=_N
    bysort firmid: egen t_miss=total(miss_revenue)
    gen dum=1
    replace dum=0 if t_miss == t_obs
    drop miss_revenue t_obs t_miss
    list

    Comment


    • #3
      Originally posted by Chen Samulsion View Post
      Code:
      set obs 30
      egen firmid=seq(), from(1) to(3) block(10)
      egen year=seq(), from(2001) to(2010)
      gen revenue=runiform()
      replace revenue=. if firmid==1 & year==2002
      replace revenue=. if firmid==3
      
      misstable summarize revenue, generate(miss_)
      bysort firmid: gen t_obs=_N
      bysort firmid: egen t_miss=total(miss_revenue)
      gen dum=1
      replace dum=0 if t_miss == t_obs
      drop miss_revenue t_obs t_miss
      list
      This works. Thank you!

      Comment


      • #4
        Clumsy fingers on a phone
        More coffee needed
        Make bad haiku
        Last edited by Nick Cox; 11 Nov 2021, 01:17.

        Comment


        • #5
          Code:
          bysort firmid (revenue) : gen wanted = !(missing(revenue[1]) & missing(revenue[_N]))
          If all values are missing, then after sorting the first and last are both missing. That being true is the negation of what is wanted.

          Comment


          • #6
            Good code, but `"CNM"' is dirty language in some languages.

            Comment

            Working...
            X