Announcement

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

  • Coding all subsequent observations per group after an event

    Hello everyone,

    I have an unbalanced daily panel data which looks like below. "Pastdefault" and "No_pastdefault" are the 2 variables that I want to create.

    a. If a seller defaults at time t, "pastdefault" will equal 1 for all subsequent observations. I have tried the codes below, but it just sets 1 for the t+1 observations.

    forvalues i = 1/`=_n' {
    2. by sellerid: gen pastdefault=1 if default[_n-`i']==1
    3. }

    b. I also need to count the number of times a seller has defaulted and create "No_pastdefault" as in the table.

    Please could anyone help me with these two questions? Thank you very much.


    TradeID SellerID time Default Pastdefault No_pastdefault
    11 1 12/01/2015 0 0 0
    12 1 15/01/2015 1 0 0
    13 1 20/06/2016 0 1 1
    14 1 30/09/2016 1 1 1
    15 1 14/01/2017 0 1 2
    21 2 10/02/2014 1 0 0
    22 2 15/02/2014 0 1 1

  • #2
    It would be better to give an example of your data with dataex (see FAQ). I took some time to create one from your table. I believe this does what you want.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(TradeID SellerID) str10 time byte(Default Pastdefault No_pastdefault)
    11 1 "12/01/2015" 0 0 0
    12 1 "15/01/2015" 1 0 0
    13 1 "20/06/2016" 0 1 1
    14 1 "30/09/2016" 1 1 1
    15 1 "14/01/2017" 0 1 2
    21 2 "10/02/2014" 1 0 0
    22 2 "15/02/2014" 0 1 1
    end
    
    gen date = date(time, "DMY")
    format date %td
    
    bysort SellerID (date): gen wanted_a = sum(Default[_n-1]) > 0
    bysort SellerID (date): gen wanted_b = sum(Default[_n-1])
    list, noobs abbr(14) sepby(SellerID)
    
      +------------------------------------------------------------------------------------------------------------+
      | TradeID   SellerID         time   Default   Pastdefault   No_pastdefault        date   wanted_a   wanted_b |
      |------------------------------------------------------------------------------------------------------------|
      |      11          1   12/01/2015         0             0                0   12jan2015          0          0 |
      |      12          1   15/01/2015         1             0                0   15jan2015          0          0 |
      |      13          1   20/06/2016         0             1                1   20jun2016          1          1 |
      |      14          1   30/09/2016         1             1                1   30sep2016          1          1 |
      |      15          1   14/01/2017         0             1                2   14jan2017          1          2 |
      |------------------------------------------------------------------------------------------------------------|
      |      21          2   10/02/2014         1             0                0   10feb2014          0          0 |
      |      22          2   15/02/2014         0             1                1   15feb2014          1          1 |
      +------------------------------------------------------------------------------------------------------------+
    Last edited by Wouter Wakker; 12 Nov 2019, 09:43.

    Comment


    • #3
      Thank you so much Wouter. It works. Next time I will post with dataex. Thank you for your time.

      Comment

      Working...
      X