Announcement

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

  • How to create a dummy variable over time and by groups (panel data - historical business segments WRDS)?

    Hi, I would like to know how I can create a dummy variable called "new_segment" that takes on the value of 1 each time a firm starts operating a new business segment and 0 otherwise. For example: for firm 1 the dummy variable will take on the value of 1 in 2023 that is when it starts operating for the first time in segment "S3". In years 2021 and 2022 the dummy variable takes on 0.
    Firm id Segment id year
    1 S1 2021
    1 S2 2021
    1 S1 2022
    1 S2 2022
    1 S1 2023
    1 S2 2023
    1 S3 2023
    2…
    More precicesly, I need to get to this:
    Firm id year New_segment
    1 2021 0
    1 2022 0
    1 2023 1
    2…
    Thanks in advance for your suggestions!

  • #2
    This may help

    Code:
    clear 
    input Firm_id    str2 Segment_id    year
    1    S1    2021
    1    S2    2021
    1    S1    2022
    1    S2    2022
    1    S1    2023
    1    S2    2023
    1    S3    2023
    end 
    bysort Firm_id Segment_id (year) : gen New = _n == 1 & year > 2021  
    bysort Firm_id year (New) : replace New = _n == 1 & New[_N] 
    
    collapse (max) New , by(Firm_id year) 
    
    list
    
         +----------------------+
         | Firm_id   year   New |
         |----------------------|
      1. |       1   2021     0 |
      2. |       1   2022     0 |
      3. |       1   2023     1 |
         +----------------------+

    Comment


    • #3
      Thanks, Nick. I worked perfectly!

      Would you mind giving some hints on how to do the opposite: to create a dummy variable when a firm stops operating in a business segment?

      I would like to create a dummy variable that takes on the value of 1 when the two conditions happen: 1. A firm close a business segment and 2. The firm does not have any other segment in the same industry. Otherwise it takes on the value of 0. For instance:
      Firm id Segment id industry year
      1 S1 1 2021
      1 S2 2 2021
      1 S1 1 2022
      1 S2 2 2022
      1 S1 1 2023
      1 S3 3 2023
      In the table above firm 1 closes segment "S2" in year 2023 and the firm does not have any other segment in industry "2", then in year 2023 the dummy should take on the value 1 and in 2022 and 2021 should take on the value 0. Like this:
      Firm id year Close_segment
      1 2021 0
      1 2022 0
      1 2023 1
      Thanks in advance!

      Comment


      • #4
        Please use dataex as illustrated in #2 and explained at https://www.statalist.org/forums/help#stata.

        Comment


        • #5
          Sure:
          Code:
          clear
          input Firm_id str2 Segment_id industry year
          1 S1 1 2021
          1 S2 2 2021
          1 S1 1 2022
          1 S2 2 2022
          1 S1 1 2023
          1 S3 3 2023
          end
          Last edited by Josue Solis; 10 Nov 2023, 14:39.

          Comment


          • #6
            This may help.

            Code:
            clear
            input Firm_id str2 Segment_id industry year
            1 S1 1 2021
            1 S2 2 2021
            1 S1 1 2022
            1 S2 2 2022
            1 S1 1 2023
            1 S3 3 2023
            end
            
            bysort Firm_id Segment_id (year) : gen last = year[_N] < 2023
            bysort Firm_id (last) : gen wanted = year == 2023 & last[_N] == 1
            sort Firm_id year Segment_id
            
            list, sepby(year)
            
            clear
            input Firm_id str2 Segment_id industry year
            1 S1 1 2021
            1 S2 2 2021
            1 S1 1 2022
            1 S2 2 2022
            1 S1 1 2023
            1 S3 3 2023
            end
            
            bysort Firm_id Segment_id (year) : gen last = year[_N] < 2023
            bysort Firm_id (last) : gen wanted = year == 2023 & last[_N] == 1
            sort Firm_id year Segment_id
            
            list, sepby(year)
            
                 +------------------------------------------------------+
                 | Firm_id   Segmen~d   industry   year   last   wanted |
                 |------------------------------------------------------|
              1. |       1         S1          1   2021      0        0 |
              2. |       1         S2          2   2021      1        0 |
                 |------------------------------------------------------|
              3. |       1         S1          1   2022      0        0 |
              4. |       1         S2          2   2022      1        0 |
                 |------------------------------------------------------|
              5. |       1         S1          1   2023      0        1 |
              6. |       1         S3          3   2023      0        1 |
                 +------------------------------------------------------+
            
            .

            Comment


            • #7
              The repetition of code in #6 was accidental. Sorry about any puzzlement.

              Comment

              Working...
              X