Announcement

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

  • Panel Data: Replace a value for observations "through time"

    Hi.

    Suppose I have a simple panel of firms, and I generate a variable "subsid2012" which is equal to 1 if the firm has received subsidy in the year 2012 and zero otherwise.
    How can I make the values of the variable "subsid2012" equal to one for those firms for all years? (not only 2012)

    In short, I want a dummy that indicates which firms have received the subsidy in the year 2012, regardless of the year I am looking at. (Something like the treatment variable in classic difference-in-difference regression.)


  • #2
    Mahdi:
    you may want to consider:
    Code:
    . set obs 2
    Number of observations (_N) was 0, now 2.
    
    . g id=1
    
    . g year=2012 in 1
    
    
    . replace year=2013 if year==.
    
    
    . expand 2
    
    
    . replace id=2 in 3/4
    
    
    . bysort id (year): gen subsid_2012=1 if id==1 & year==2012
    
    
    . replace subsid_2012=0 if subsid_2012==.
    
    
    . bysort id (year) subsid_2012: replace subsid_2012=1 if subsid_2012[1]==1
    
    
    . list
    
         +----------------------+
         | id   year   sub~2012 |
         |----------------------|
      1. |  1   2012          1 |
      2. |  1   2013          1 |
      3. |  2   2012          0 |
      4. |  2   2013          0 |
         +----------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      The correspondence any <=> max, all <=> min is worth remembering as a key to many such problems. See https://www.stata.com/support/faqs/d...ble-recording/ for the main ideas.


      Here's an analogue: for the Grunfeld data, we want indicator 1 if a company had invest > 100 in 1945 and 0 otherwiseThat turns out to be companies 1 and 2.


      Code:
      . webuse grunfeld, clear
      
      . list company invest if year == 1945
      
           +------------------+
           | company   invest |
           |------------------|
       11. |       1    561.2 |
       31. |       2    258.7 |
       51. |       3     93.6 |
       71. |       4    88.78 |
       91. |       5    63.21 |
           |------------------|
      111. |       6    39.03 |
      131. |       7    44.12 |
      151. |       8    39.27 |
      171. |       9    52.32 |
      191. |      10     1.36 |
           +------------------+
      
      
      . egen wanted = max(invest > 100 & year == 1945), by(company)
      
      
      
      . l company if wanted
      
           +---------+
           | company |
           |---------|
        1. |       1 |
        2. |       1 |
        3. |       1 |
        4. |       1 |
        5. |       1 |
           |---------|
        6. |       1 |
        7. |       1 |
        8. |       1 |
        9. |       1 |
       10. |       1 |
           |---------|
       11. |       1 |
       12. |       1 |
       13. |       1 |
       14. |       1 |
       15. |       1 |
           |---------|
       16. |       1 |
       17. |       1 |
       18. |       1 |
       19. |       1 |
       20. |       1 |
           |---------|
       21. |       2 |
       22. |       2 |
       23. |       2 |
       24. |       2 |
       25. |       2 |
           |---------|
       26. |       2 |
       27. |       2 |
       28. |       2 |
       29. |       2 |
       30. |       2 |
           |---------|
       31. |       2 |
       32. |       2 |
       33. |       2 |
       34. |       2 |
       35. |       2 |
           |---------|
       36. |       2 |
       37. |       2 |
       38. |       2 |
       39. |       2 |
       40. |       2 |
           +---------+
      
      .
      See also https://www.stata-journal.com/articl...article=dm0055


      EDIT

      In your case something like

      Code:
      bysort firmid (subsid2012) : gen wanted = subsid2012[_N]
      may be enough.
      Last edited by Nick Cox; 19 May 2022, 04:56.

      Comment


      • #4
        Great. Thanks a lot.

        Comment

        Working...
        X